Camera1 API
权限申请
AndroidManifest.xml 记住是 <uses-feature>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
再申请存储权限,反正也要保存图片
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代码也申请,毕竟android 6.0需要动态申请权限
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//没有存储权限则申请权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//申请相机权限
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
}
拍照步骤
- 获得Camera 实例 Camera camera;
- open(int)
- 获取默认配置 Camera.Parameters parameters = camera.getParameters();
- 修改参数后(预览分辨率,帧率,拍照分辨率等) camera.setParameters(Camera.Parameters)
- 调用 setDisplayOrientation(int) 确保预览方向
- 重要:确保初始化 SurfaceHolder to camera.setPreviewDisplay(SurfaceHolder);没用SurfaceHolder,Camera 将不会开始预览
- 重要: 调用camera.startPreview()开始预览
- 如果你想拍摄图片,调用:takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)
- 当你调用takePicture()后,预览已经停止了,这时还需要调用camera.startPreview()重新预览
- 调用 stopPreview() 停止预览
- 重要:应用转向其他界面或其他应用,应立即在 Activity.onPause() 调用release() 恢复在Activity.onResume()调用重新打开
在开发板上调试AHD模组
不能调用takePicture() 获取图片,这样通过YUV压缩出来会有失真。
所以用的在onPreview()这里取的数据进行压缩保存
代码
package com.example.megcar.camera;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Handler;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
SurfaceView sView;
SurfaceHolder surfaceHolder;
int screenWidth = 1280;
int screenHeight = 720;
// 定义系统所用的照相机
Camera camera;
// 是否在预览中
boolean isPreview = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 设置全屏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
// 获取窗口管理器
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
// screenWidth = metrics.widthPixels;
// screenHeight = metrics.heightPixels;
Log.e(TAG, "onCreate: screenWidth = " + dm.widthPixels + " screenHeight = " + dm.heightPixels);
// 获取界面中SurfaceView组件
sView = (SurfaceView) findViewById(R.id.sView);
// 设置该Surface不需要自己维护缓冲区
sView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 获得SurfaceView的SurfaceHolder
surfaceHolder = sView.getHolder();
// 为surfaceHolder添加一个回调监听器
surfaceHolder.addCallback(new Callback()
{
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height)
{
Log.e(TAG, "surfaceChanged: " );
initCamera();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
// 打开摄像头
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// 如果camera不为null ,释放摄像头
if (camera != null)
{
Log.e(TAG, "surfaceDestroyed: " );
if (isPreview) camera.stopPreview();
camera.release();
camera = null;
isPreview = false;
}
}
});
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//没有权限则申请权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
}
}
}
@Override
protected void onResume() {
super.onResume();
// if (camera == null && isPreview ) {
// Log.e(TAG, "onResume: startPreview" );
// initCamera();
// }
}
@Override
protected void onStop() {
super.onStop();
if (camera != null && isPreview ) {
camera.stopPreview();
camera = null;
isPreview = false;
}
}
private void initCamera()
{
if (!isPreview)
{
// 此处默认打开后置摄像头。
// 通过传入参数可以打开前置摄像头
camera = Camera.open(1); //①
camera.setDisplayOrientation(90);
Log.e(TAG, "initCamera: open " );
List<Camera.Size> pictureSizes = camera.getParameters().getSupportedPictureSizes();
for (int i=0; i<pictureSizes.size(); i++) {
Camera.Size pSize = pictureSizes.get(i);
Log.i(TAG, "----------PictureSize.width = "+pSize.width+"-----------------PictureSize.height = "+pSize.height);
}
}
if (camera != null && !isPreview)
{
try
{
Camera.Parameters parameters = camera.getParameters();
// 设置预览照片的大小
parameters.setPreviewSize(screenWidth, screenHeight);
// 设置预览照片时每秒显示多少帧的最小值和最大值
// parameters.setPreviewFpsRange(4, 10);
// 设置图片格式
parameters.setPictureFormat(ImageFormat.NV21);
// 设置JPG照片的质量
// parameters.set("jpeg-quality", 80);
// 设置照片的大小
parameters.setPictureSize(screenWidth, screenHeight);
List<String> focusModes = parameters.getSupportedFocusModes();
for (String mode : focusModes) {
if (mode.equals(Camera.Parameters.FOCUS_MODE_AUTO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
break;
}
}
camera.setParameters(parameters);
camera.addCallbackBuffer(new byte[((screenWidth * screenHeight) * ImageFormat.getBitsPerPixel(ImageFormat.NV21)) / 8]);
camera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// Log.e(TAG, "onPreviewFrame " );
if (take_picture) {
Log.e(TAG, "onPreviewFrame:take_picture " );
final byte[] yuv = data;
new Thread(new Runnable() {
@Override
public void run() {
SaveNV21Bitmap(yuv);
}
}).start();
take_picture = false;
}
camera.addCallbackBuffer(new byte[data.length]);
}
});
// 通过SurfaceView显示取景画面
camera.setPreviewDisplay(surfaceHolder); //②
// 开始预览
camera.startPreview(); //③
Log.e(TAG, "initCamera: startPreview" );
}
catch (Exception e)
{
e.printStackTrace();
}
isPreview = true;
}
}
private boolean take_picture = false;
public void capture(View source)
{
// if (camera != null)
// {
// // 控制摄像头自动对焦后才拍照
// camera.autoFocus(autoFocusCallback); //④
// }
// camera.takePicture(null, null, new Camera.PictureCallback(){
// @Override
// public void onPictureTaken(byte[] data, Camera mcamera) {
// savePicture(data);
// mcamera.stopPreview();
// mcamera.startPreview();
// isPreview = true;
// }
// });
Log.e(TAG, "capture: " );
take_picture = true;
}
AutoFocusCallback autoFocusCallback = new AutoFocusCallback()
{
// 当自动对焦时激发该方法
@Override
public void onAutoFocus(boolean success, Camera camera)
{
if (success)
{
Log.e(TAG, "onAutoFocus: sucess" );
camera.takePicture(null, null, new Camera.PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera mcamera) {
savePicture(data);
mcamera.stopPreview();
mcamera.startPreview();
isPreview = true;
}
});
}
Log.e(TAG, "onAutoFocus: fail" );
}
};
PictureCallback myJpegCallback = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
// 重新浏览
camera.stopPreview();
camera.startPreview();
isPreview = true;
}
};
/**
* get current date and time as String
* @return
*/
private static final String getDateTimeString() {
final GregorianCalendar now = new GregorianCalendar();
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US).format(now.getTime());
}
private File getCaptureFile() {
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"Camera1");
Log.d(TAG, "path=" + dir.toString());
if (!dir.exists()) {
dir.mkdir();
}
if (dir.canWrite()) {
return new File(dir,getDateTimeString() + ".jpg");
}
return null;
}
private void SaveNV21Bitmap(byte[] data) {
File pictureFile = getCaptureFile();
try {
FileOutputStream filecon = new FileOutputStream(pictureFile);
YuvImage image = new YuvImage(data, ImageFormat.NV21, screenWidth, screenHeight, null); //将NV21 data保存成YuvImage
//图像压缩
image.compressToJpeg(
new Rect(0, 0, image.getWidth(), image.getHeight()),
80, filecon); // 将NV21格式图片,以质量70压缩成Jpeg,并得到JPEG数据流
}catch (IOException e)
{
e.printStackTrace();
}
}
public void saveByteToImage(final byte[] data,File file) {
if (data == null || file == null)
return;
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);
}catch (FileNotFoundException e){
Log.e(TAG, "saveByteToImage: " + e.getMessage() );
}
}
private void savePicture(byte[] data) {
File pictureFile = getCaptureFile();
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(pictureFile));
bos.write(data);
bos.flush();
Toast.makeText(this,"拍照成功",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}