Android实现Camera2预览和拍照效果

这篇文章主要为大家详细介绍了Android开发之一个类实现Camera2预览和拍照效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

简介

网上对于 Camera2 的介绍有很多,在 Github 上也有很多关于 Camera2 的封装库,但是对于那些库,封装性太强,有时候我们仅仅是需要个简简单单的拍照功能而已,因此,自定义一个 Camera 使之变得轻量级那是非常重要的了。(本文并非重复造轮子, 而是在于学习 Camera2API 的基本功能, 笔记之。)

学习要点:

使用 Android Camera2 API 的基本功能。
迭代连接到设备的所有相机的特征。
显示相机预览和拍摄照片。

Camera2 API 为连接到 Android 设备的各个相机设备提供了一个界面。 它替代了已弃用的 Camera 类。

  • 使用 getCameraIdList 获取所有可用摄像机的列表。 然后,您可以使用 getCameraCharacteristics,并找到适合您需要的最佳相机(前 / 后面,分辨率等)。
  • 创建一个 CameraDevice.StateCallback 的实例并打开相机。 当相机打开时,准备开始相机预览。
  • 使用 TextureView 显示相机预览。 创建一个 CameraCaptureSession 并设置一个重复的 CaptureRequest。
  • 静像拍摄需要几个步骤。 首先,需要通过更新相机预览的 CaptureRequest 来锁定相机的焦点。
  • 然后,以类似的方式,需要运行一个预捕获序列。之后,它准备拍摄一张照片。 创建一个新的 CaptureRequest 并调用 [capture] 。

完成后,别忘了解锁焦点。

实现效果

环境

SDK>21

Camera2 类图

代码实现

CameraPreview.java

 /** * Created by shenhua on 2017-10-20-0020. * Email shenhuanet@126.com */ public class CameraPreview extends TextureView { private static final String TAG = "CameraPreview"; private static final SparseIntArray ORIENTATIONS = new SparseIntArray();//从屏幕旋转转换为JPEG方向 private static final int MAX_PREVIEW_WIDTH = 1920;//Camera2 API 保证的最大预览宽高 private static final int MAX_PREVIEW_HEIGHT = 1080; private static final int STATE_PREVIEW = 0;//显示相机预览 private static final int STATE_WAITING_LOCK = 1;//焦点锁定中 private static final int STATE_WAITING_PRE_CAPTURE = 2;//拍照中 private static final int STATE_WAITING_NON_PRE_CAPTURE = 3;//其它状态 private static final int STATE_PICTURE_TAKEN = 4;//拍照完毕 private int mState = STATE_PREVIEW; private int mRatioWidth = 0, mRatioHeight = 0; private int mSensorOrientation; private boolean mFlashSupported; private Semaphore mCameraOpenCloseLock = new Semaphore(1);//使用信号量 Semaphore 进行多线程任务调度 private Activity activity; private File mFile; private HandlerThread mBackgroundThread; private Handler mBackgroundHandler; private Size mPreviewSize; private String mCameraId; private CameraDevice mCameraDevice; private CaptureRequest.Builder mPreviewRequestBuilder; private CaptureRequest mPreviewRequest; private CameraCaptureSession mCaptureSession; private ImageReader mImageReader; static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } public CameraPreview(Context context) { this(context, null); } public CameraPreview(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CameraPreview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mFile = new File(getContext().getExternalFilesDir(null), "pic.jpg-600"); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); if (0 == mRatioWidth || 0 == mRatioHeight) { setMeasuredDimension(width, height); } else { if (width  MAX_PREVIEW_WIDTH) { maxPreviewWidth = MAX_PREVIEW_WIDTH; } if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) { maxPreviewHeight = MAX_PREVIEW_HEIGHT; } mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest); int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) { setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight()); } else { setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth()); } Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); mFlashSupported = available == null ? false : available; mCameraId = cameraId; return; } } catch (CameraAccessException e) { e.printStackTrace(); } catch (NullPointerException e) { Log.e(TAG, "设备不支持Camera2"); } } /** * 获取一个合适的相机预览尺寸 * * @param choices      支持的预览尺寸列表 * @param textureViewWidth 相对宽度 * @param textureViewHeight 相对高度 * @param maxWidth     可以选择的最大宽度 * @param maxHeight     可以选择的最大高度 * @param aspectRatio    宽高比 * @return 最佳预览尺寸 */ private static Size chooseOptimalSize(Size[] choices, int textureViewWidth, int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) { List bigEnough = new ArrayList<>(); List notBigEnough = new ArrayList<>(); int w = aspectRatio.getWidth(); int h = aspectRatio.getHeight(); for (Size option : choices) { if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight && option.getHeight() == option.getWidth() * h / w) { if (option.getWidth() >= textureViewWidth && option.getHeight() >= textureViewHeight) { bigEnough.add(option); } else { notBigEnough.add(option); } } } if (bigEnough.size() > 0) { return Collections.min(bigEnough, new CompareSizesByArea()); } else if (notBigEnough.size() > 0) { return Collections.max(notBigEnough, new CompareSizesByArea()); } else { Log.e(TAG, "Couldn't find any suitable preview size"); return choices[0]; } } /** * 为相机预览创建新的CameraCaptureSession */ private void createCameraPreviewSession() { try { SurfaceTexture texture = this.getSurfaceTexture(); assert texture != null; // 将默认缓冲区的大小配置为想要的相机预览的大小 texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface surface = new Surface(texture); mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mPreviewRequestBuilder.addTarget(surface); // 我们创建一个 CameraCaptureSession 来进行相机预览 mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { if (null == mCameraDevice) { return; } // 会话准备好后,我们开始显示预览 mCaptureSession = cameraCaptureSession; try { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); setAutoFlash(mPreviewRequestBuilder); mPreviewRequest = mPreviewRequestBuilder.build(); mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * 从指定的屏幕旋转中检索照片方向 * * @param rotation 屏幕方向 * @return 照片方向(0,90,270,360) */ private int getOrientation(int rotation) { return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360; } /** * 锁定焦点 */ private void lockFocus() { try { // 如何通知相机锁定焦点 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); // 通知mCaptureCallback等待锁定 mState = STATE_WAITING_LOCK; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * 解锁焦点 */ private void unlockFocus() { try { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); setAutoFlash(mPreviewRequestBuilder); mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); mState = STATE_PREVIEW; mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * 拍摄静态图片 */ private void captureStillPicture() { try { if (null == activity || null == mCameraDevice) { return; } final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(mImageReader.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); setAutoFlash(captureBuilder); // 方向 int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation)); CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { Toast.makeText(getContext(), "Saved: " + mFile, Toast.LENGTH_SHORT).show(); Log.d(TAG, mFile.toString()); unlockFocus(); } }; mCaptureSession.stopRepeating(); mCaptureSession.abortCaptures(); mCaptureSession.capture(captureBuilder.build(), captureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * 运行preCapture序列来捕获静止图像 */ private void runPreCaptureSequence() { try { // 设置拍照参数请求 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); mState = STATE_WAITING_PRE_CAPTURE; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } /** * 比较两者大小 */ private static class CompareSizesByArea implements Comparator { @Override public int compare(Size lhs, Size rhs) { return Long.signum((long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight()); } } /** * ImageReader的回调对象 */ private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile)); } }; /** * 将捕获到的图像保存到指定的文件中 */ private static class ImageSaver implements Runnable { private final Image mImage; private final File mFile; ImageSaver(Image image, File file) { mImage = image; mFile = file; } @Override public void run() { ByteBuffer buffer = mImage.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); FileOutputStream output = null; try { output = new FileOutputStream(mFile); output.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { mImage.close(); if (null != output) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } 

MainActivity.java

 public class MainActivity extends AppCompatActivity { CameraPreview cameraView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cameraView = (CameraPreview) findViewById(R.id.cameraView); } @Override protected void onResume() { super.onResume(); cameraView.onResume(this); } @Override protected void onPause() { cameraView.onPause(); super.onPause(); } public void takePic(View view) { cameraView.takePicture(); } } 

activity_main.xml

  

资源文件 ic_capture_200px.xml

  

其它

Manifest 权限:

 

Android6.0 运行时权限未贴出。(注意:为了方便读者手机端阅读,本文代码部分的成员变量使用了行尾注释,在正常编程习惯中,请使用 /* / 注释。)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是Android实现Camera2预览和拍照效果的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动