实现功能:
1、通过SurfaceView进行摄像头的预览
2、将拍照的照片存储到指定的位置
3、拍照后的照片用ImageView显示出来
activity_custom_camera.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CustomCameraActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takePhoto"
android:text="take photo" />
<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" />
</RelativeLayout>
</LinearLayout>
CustomCameraActivity代码
import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
public class CustomCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView preView;
private Camera mCamera;
private SurfaceHolder mHolder;
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
long currentTimeMillis = System.currentTimeMillis();
String fileName = "/sdcard/" + currentTimeMillis + ".png";
try {
//data保存拍照后的所有数据
File tempFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(data);
fos.close();
Intent intent = new Intent(CustomCameraActivity.this, ResultAtyActivity.class);
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);
preView = (SurfaceView) findViewById(R.id.preview);
mHolder = preView.getHolder();
mHolder.addCallback(this);
preView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.autoFocus(null);
}
});
}
public void takePhoto(View v) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPreviewSize(800, 400);
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) {
mCamera = getCamera();
if (mHolder != null) {
setStartPreview(mCamera, mHolder);
}
}
}
@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
/**
* 获取Camera对象
*
* @return
*/
private Camera getCamera() {
Camera camera;
try {
camera = Camera.open();
} catch (Exception e) {
camera = null;
e.printStackTrace();
}
return camera;
}
/**
* 开始显示Camera的预览
*/
private void setStartPreview(Camera camera, SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
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();
}
}
activity_result.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ResultAtyActivity">
<TextView
android:id="@+id/path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="000"
android:textSize="16sp" />
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ResultAtyActivity代码
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ResultAtyActivity extends AppCompatActivity {
ImageView pic;
TextView pathTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_aty);
String path = getIntent().getStringExtra("picPath");
Log.e("info","获取到的地址:"+ path);
pathTv = (TextView) findViewById(R.id.path);
pathTv.setText(path);
pic = (ImageView) findViewById(R.id.pic);
try {
FileInputStream fis = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
Matrix matrix = new Matrix();
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);
}
}
代码还是比较简单的, 应该能看懂。
附上DEMO下载地址
DEMO下载
https://download.csdn.net/download/qi85481455/11195599
上传DEMO的时候不知道为啥默认5积分,好像也不能修改, 需要下载没有积分的童鞋,可私聊我,我给你下载