Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab08 #94 #726

Merged
merged 2 commits into from
May 12, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
lab08 #94
Hackergeek committed May 12, 2016
commit 3359b899614ce0163a0a44a26b10a88fac4e2688
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package skyward.com.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 自定义相机
* 1.创建Camera对象
* 2.创建SurfaceView对象
* 3.关联Camera对象和SurfaceView对象
* 4.调整相机的显示效果
* 5.自定义相机预览界面
* Created by skyward on 2016/4/29.
*/
public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback{
private static final String TAG = "CustomCameraActivity";
private Camera mCamera;
private SurfaceView mPreview;
private SurfaceHolder mHolder;
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//生成照片
File tempFile = new File("/sdcard/temp.png");
try {
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(data);
fos.close();
Intent intent = new Intent(CustomCameraActivity.this, ResultActivity.class);
Log.d(TAG, "onPictureTaken: " + tempFile.getAbsolutePath());
intent.putExtra("picPath", tempFile.getAbsolutePath());
startActivity(intent);
CustomCameraActivity.this.finish();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_camera);
//2.创建SurfaceView对象
mPreview = (SurfaceView) findViewById(R.id.preview);
// 点击屏幕时自动对焦
mPreview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.autoFocus(null);
}
});
//获取SurfaceHolder对象
mHolder = mPreview.getHolder();
mHolder.addCallback(this);
}

/**
* 拍照
* @param view
*/
public void capture(View view) {
//4.调整相机的显示效果
//参数设置
Camera.Parameters parameters = mCamera.getParameters();
//设置照片格式
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPreviewSize(1280, 720);
//设置对焦模式为自动对焦
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
//当对焦成功,则拍照
if (success) {
mCamera.takePicture(null, null, mPictureCallback);
}
}
});
}

@Override
protected void onResume() {
super.onResume();
if(mCamera == null) {
// 1.创建Camera对象
mCamera = getCamera();
if (mHolder != null) {
setStartPreview(mCamera, mHolder);
}
}
}

@Override
protected void onPause() {
super.onPause();
releaseCamera();
}

/**
* 获取系统Camera对象
* @return
*/
private Camera getCamera() {
Camera camera;
try {
camera = Camera.open();
} catch (Exception e) {
camera = null;
e.printStackTrace();
}
return camera;
}

/**
* 开始预览相机内容
*/
private void setStartPreview(Camera camera, SurfaceHolder holder) {
try {
//3.关联Camera对象和SurfaceView对象
camera.setPreviewDisplay(holder);
//系统默认横屏显示
//因此要将系统camera预览角度调整90度
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 释放相机资源
*/
private void releaseCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
setStartPreview(mCamera, mHolder);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mCamera.stopPreview();
setStartPreview(mCamera, mHolder);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package skyward.com.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
*
*/
public class MActivity extends AppCompatActivity {
public static final int REQUEST_CODE_1 = 1;
public static final int REQUEST_CODE_2 = 2;
private static final String TAG = "MActivity";
//文件路径
private String mFilePath;

private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m);
iv = (ImageView) findViewById(R.id.iv);
//获取SD卡路径(外部存储路径) /storage/emulated/0
mFilePath = Environment.getExternalStorageDirectory().getPath();
Log.d(TAG, "onCreate: " + mFilePath);
//获取公共外部存储空间路径 /storage/emulated/0/Pictures
// File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// Log.d(TAG, "onCreate: " + file.getAbsolutePath());
mFilePath = mFilePath + "/temp.png";
}

/**
* 调用系统相机(返回缩略图)
* @param view
*/
public void startCamera1(View view) {
//通过隐式intent启动相机应用
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_1);
}

/**
* 调用系统相机(返回原图)
* @param view
*/
public void startCamera2(View view) {
//通过隐式intent启动相机应用
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(new File(mFilePath));
//修改系统保存图片的路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, REQUEST_CODE_2);
}

/**
* 调用自定义相机
* @param view
*/
public void customCamera(View view) {
Intent intent = new Intent(MActivity.this, CustomCameraActivity.class);
startActivity(intent);
}

//将照片添加至系统相册
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mFilePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == REQUEST_CODE_1) {
/**
* 系统相机默认返回的bitmap为缩略图
*/
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
iv.setImageBitmap(bitmap);
} else if(requestCode == REQUEST_CODE_2) {
/**
* 要想使系统相机返回原图
* 1.修改系统默认保存照片路径
* 2.自定义照片文件保存路径,在调用系统相机之前,将路径作为参数传递给系统相机
* 3.创建文件输入流,读取照片文件并转换为Bitmap,显示在ImageView中
*
* 有可能会因为照片文件过大以致抛出内存溢出异常,不能够将照片显示在ImageView中
*/
try {
FileInputStream fis = new FileInputStream(mFilePath);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
Log.d(TAG, "onActivityResult: 读取图片");
iv.setImageBitmap(bitmap);
galleryAddPic();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {

}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package skyward.com.camera;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
* 5.自定义相机预览界面
* 相片预览界面
*/
public class ResultActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//获取相片所在的路径
String path = getIntent().getStringExtra("picPath");
ImageView pic = (ImageView) findViewById(R.id.pic);
//系统默认拍摄的相片为横屏显示,因此要将图片旋转90度后再显示
try {
FileInputStream fis = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
Matrix matrix = new Matrix();
//将图片旋转90度
matrix.setRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
pic.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Bitmap bitmap = BitmapFactory.decodeFile(path);
// pic.setImageBitmap(bitmap);
}
}
33 changes: 33 additions & 0 deletions app/src/main/res/layout/activity_custom_camera.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="capture"
android:onClick="capture"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="welcom to My Camera"
android:gravity="center"
android:layout_marginBottom="80dp"
android:textColor="#e40e31"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

</LinearLayout>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/activity_m.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="skyward.com.camera.MActivity">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用系统相机(返回缩略图)"
android:onClick="startCamera1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用系统相机(返回原图)"
android:onClick="startCamera2"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用自定义相机"
android:onClick="customCamera"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv"/>
</LinearLayout>
21 changes: 21 additions & 0 deletions app/src/main/res/layout/activity_result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="skyward.com.camera.ResultActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Capture Result"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pic"
android:scaleType="center"/>

</LinearLayout>