android camera2
1、camera2api的部分描述:
|
api地址:https://developer.android.com/reference/android/hardware/camera2/package-summary.html
用于捕捉相机图像或从相机中捕捉到的图像的摄像机的配置捕捉会话 |
CameraCaptureSession.CaptureCallback |
用于跟踪提交给相机设备的CaptureRequest进程的回调对象。 |
CameraCaptureSession.StateCallback | 一个回调对象,用于接收关于摄像机捕获会话状态的更新。 |
CameraCharacteristics |
描述一个摄像机的属性。 |
CameraCharacteristics.Key<T> | 相机特性 |
CameraConstrainedHighSpeedCaptureSession | 一种受限的高速捕获通道,用于捕捉高速视频记录用例的高速图像。 |
CameraDevice |
这款相机是一款与Android设备相连的单机相机,可以在高帧率下对图像捕捉和后处理进行精细控制 |
CameraDevice.StateCallback | 一个回调对象,用于接收有关相机设备状态的更新。 |
CameraManager |
|
CameraManager.AvailabilityCallback | 于摄像机设备的回调,描述相机可用或无法打开. |
CameraManager.TorchCallback | 摄像机闪光灯模式的回调,即不可用、禁用或启用。 |
CameraMetadata<TKey> | 相机控制和信息的基类 |
CaptureFailure | 图像传感器捕获单个图像捕获失败. |
CaptureRequest |
一个不可变的设置和输出包,需要从摄像机设备中捕获一个图像 |
CaptureRequest.Builder | 构建器捕获请求 |
CaptureRequest.Key<T> | 个键用于捕获请求字段查找 get(CaptureResult.Key) 或设置 set(Key, Object) . |
CaptureResult |
从图像传感器中捕获的单个图像的子集 |
CaptureResult.Key<T> | 捕获结果字段 |
DngCreator | dng主类提供了将原始像素数据写入DNG文件的功能。 |
TotalCaptureResult |
从图像传感器中捕获的单个图像的总聚集结果 |
2、详见代码:
package com.admin.smartcam.androidcamera.camera2; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.graphics.SurfaceTexture; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CaptureRequest; import android.media.CamcorderProfile; import android.media.MediaRecorder; import android.os.Build; import android.os.Environment; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.view.Surface; import android.view.TextureView; import android.widget.Toast; import com.admin.smartcam.service.SmartCamService; import java.io.File; import java.io.IOException; import java.util.Arrays; import static android.content.Context.CAMERA_SERVICE; import static com.admin.smartcam.service.SmartCamService.handler; /** * Created by Admin on 2017/6/2 0002. */ @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class TextureViewPreview extends TextureView implements TextureView.SurfaceTextureListener { private Context context; private CameraManager cameraManager; private CameraDevice cameraDevice; public static CaptureRequest.Builder mPreviewBuilder; public static MediaRecorder mMediaRecorder; public TextureViewPreview(Context context) { super(context); this.context = context; setSurfaceTextureListener(this); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { openCamera(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } /** * 第一步 */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private boolean openCamera() { cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE);//获得相机服务 try { String[] CameraIdList = cameraManager.getCameraIdList(); //获取可用相机设备列表 CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(CameraIdList[0]); //在这里可以通过CameraCharacteristics设置相机的功能,须检查是否支持 characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); //就像这样 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { return false; } try { cameraManager.openCamera(CameraIdList[0], new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice cameraDevice) { startView(cameraDevice); } @Override public void onDisconnected(CameraDevice cameraDevice) { cameraDevice.close(); } @Override public void onError(CameraDevice cameraDevice, int i) { cameraDevice.close(); } }, handler); } catch (CameraAccessException e) { e.printStackTrace(); } return true; } catch (CameraAccessException e) { e.printStackTrace(); } return true; } /** * 第二步 * 启动预览 */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private void startView(CameraDevice cameraDevice) { //setUpMediaRecorder(); SurfaceTexture surfaceTexture=this.getSurfaceTexture(); if (surfaceTexture!=null) { Surface surface = new Surface(surfaceTexture); try { //预览请求 mPreviewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); } catch (CameraAccessException e) { e.printStackTrace(); } mPreviewBuilder.addTarget(surface); //mPreviewBuilder.addTarget(mMediaRecorder.getSurface()); try { cameraDevice.createCaptureSession(Arrays.asList(surface), new AandroidCaptureSessionCallBack(), SmartCamService.handler); } catch (CameraAccessException e) { e.printStackTrace(); } }else Toast.makeText(context,"那个为空我也很无奈啊!", Toast.LENGTH_SHORT).show(); } /** * 设置录像 **/ private void setUpMediaRecorder() { mMediaRecorder = new MediaRecorder(); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setProfile(CamcorderProfile.get(6));//设置分辨率为1080 mMediaRecorder.setOutputFile(initPath(getDate()).getAbsolutePath()); try { mMediaRecorder.prepare(); //mMediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取系统当前时间 * * @return */ private String getDate() { String savedate = null; savedate = System.currentTimeMillis() + ".mp4"; return savedate; } /** * 初始化保存路径 */ private static File initPath(String saverPath) { File filesd = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), saverPath);//在SD File file=Environment.getDataDirectory();//获取手机内存目录 if (saverPath.equals("")) { if (!filesd.exists()) { filesd.mkdirs(); } } return file; } }
package com.admin.smartcam.androidcamera.camera2; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CaptureRequest; import android.os.Build; import android.support.annotation.RequiresApi; import android.widget.Toast; import com.admin.smartcam.main.MainActivity; import com.admin.smartcam.service.SmartCamService; import static com.admin.smartcam.androidcamera.camera2.TextureViewPreview.mPreviewBuilder; import static com.admin.smartcam.main.MainActivity.count; /** * 第三步 * Created by Admin on 2017/4/23 0023. * 将处理预览和拍照图片的工作 * 摄像头采集状态回调 */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class AandroidCaptureSessionCallBack extends CameraCaptureSession.StateCallback { @Override public void onConfigured(CameraCaptureSession cameraCaptureSession) { try { mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); mPreviewBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); //cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); Toast.makeText(MainActivity.context,"AandroidCaptureSessionCallBack:>>>:"+count++, Toast.LENGTH_SHORT).show(); cameraCaptureSession.capture(mPreviewBuilder.build(), new AndroidCaptureCallback(), SmartCamService.handler);//数据回掉接口 } catch (CameraAccessException e) { e.printStackTrace(); } } @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { // cameraCaptureSession.getInputSurface().lockCanvas(new Rect(1,1,1,11)); } }
package com.admin.smartcam.androidcamera.camera2; import android.graphics.Bitmap; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.CaptureResult; import android.hardware.camera2.TotalCaptureResult; import android.os.Build; import android.support.annotation.RequiresApi; import android.util.Log; /** * 第四步 * Created by Admin on 2017/4/23 0023. * 包含两个状态,采集进行,采集完成 可以在这里面去做处理, *数据回掉接口 *摄像头采集回话采集状态的回调 */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class AndroidCaptureCallback extends CameraCaptureSession.CaptureCallback { //在onCaptureCompleted中处理数据 @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { int afState = result.get(CaptureResult.CONTROL_AF_STATE); if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState || CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState || CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED == afState) { //do something like save picture Bitmap bitmap=(Bitmap) request.getTag(); Log.d("数据处理》》", String.valueOf(bitmap.getHeight())); for (CaptureResult rs : result.getPartialResults()) { rs.getFrameNumber(); } } } @Override public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) { } }
MainActivity.java
frameLayout = (FrameLayout) findViewById(R.id.camera_FrameLayout); textureViewPreview = new TextureViewPreview(this); //textureViewPreview.setAlpha(0.5f);//设置透明度 frameLayout.addView(textureViewPreview); 布局: <FrameLayout android:id="@+id/camera_FrameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout>
用自己所知道的去帮助他人,就像别人当初帮助自己一样!