大佬教程收集整理的这篇文章主要介绍了android – 如何在SurfaceView上显示相机预览?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
码
@Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_main); this.mBtnCapture = (Button) findViewById(R.id.actMain_btn_capturE); this.mSurfaceView = (SurfaceView) findViewById(R.id.actMain_surfaceView); this.mSurfaceHolder = this.mSurfaceView.getHolder(); this.mSurfaceHolder.addCallBACk(this); this.mCameraManager = (CameraManager) this.getSystemservice(Context.CAMERA_serviCE); try { cameraIDsList = this.mCameraManager.getCameraIdList(); for (String id : cameraIDsList) { Log.v(tag,"CameraID: " + id); } } catch (CameraAccessException E) { e.printStackTrace(); } cameraStateCB = new CameraDevice.StateCallBACk() { @Override public void onOpened(CameraDevice camera) { Toast.makeText(getApplicationContext(),"onOpened",Toast.LENGTH_SHORT).show(); //requesTing permission int permission@R_674_10943@k = ContextCompat.@R_674_10943@kSelfPermission(activity,Manifest.permission.CAMERA); if (permission@R_674_10943@k != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowrequestPermissionRationale(activity,Manifest.permission.CAMERA)) { } else { ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},MY_PERMISSIONS_requEST_CAMERA); Toast.makeText(getApplicationContext(),"request permission",Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(),"PERMISSION_ALREADY_GRANTED",Toast.LENGTH_SHORT).show(); } //@R_404_1304@ the camera try { mCameraManager.openCamera(cameraIDsList[1],cameraStateCB,new Handler()); } catch (CameraAccessException E) { e.printStackTrace(); } } @Override public void onDisconnected(CameraDevice camera) { Toast.makeText(getApplicationContext(),"onDisconnected",Toast.LENGTH_SHORT).show(); } @Override public void onError(CameraDevice camera,int error) { Toast.makeText(getApplicationContext(),"onError",Toast.LENGTH_SHORT).show(); } }; CameraManager.AvailabilityCallBACk cameraAvailableCB = new CameraManager.AvailabilityCallBACk() { @Override public void onCameraAvailable(String cameraId) { super.onCameraAvailable(cameraId); Toast.makeText(getApplicationContext(),"onCameraAvailable",Toast.LENGTH_SHORT).show(); } @Override public void onCameraUnavailable(String cameraId) { super.onCameraUnavailable(cameraId); Toast.makeText(getApplicationContext(),"onCameraUnavailable",Toast.LENGTH_SHORT).show(); } }; this.mCameraManager.registerAvailabilityCallBACk(cameraAvailableCB,new Handler()); } @Override public void onrequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) { super.onrequestPermissionsResult(requestCode,permissions,grantResults); switch (requestCodE) { case MY_PERMISSIONS_requEST_CAMERA: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) // Open Camera break; } } @Override public void surfaceCreated(SurfaceHolder holder) { Log.w(tag,"surfaceCreated"); } @Override public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) { Log.w(tag,"surfaceChanged"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.w(tag,"surfaceDestroyed"); }
>获得使用相机设备的权限
>使用CameraManager打开与相机的连接
准备表面预览
>使用打开的相机设备和所需的曲面(它不仅可以@L_696_12@预览曲面)创建CaptureSession
> CaptureSession创建后,您需要创建并配置Capturerequest并将其提交给CaptureSession
需要注意的是,准备表面和打开连接到相机是独立的过程,所以您需要确保它们都是在创建CaptureSession之前完成的.
public class MainActivity extends AppCompatActivity implements SurfaceHolder.CallBACk,Handler.CallBACk { static final String TAG = "CamTest"; static final int MY_PERMISSIONS_requEST_CAMERA = 1242; private static final int MSG_CAMERA_OPENED = 1; private static final int MSG_SURFACE_READY = 2; private final Handler mHandler = new Handler(this); SurfaceView mSurfaceView; SurfaceHolder mSurfaceHolder; CameraManager mCameraManager; String[] mCameraIDsList; CameraDevice.StateCallBACk mCameraStateCB; CameraDevice mCameraDevice; CameraCaptureSession mCaptureSession; Boolean mSurfaceCreated = true; Boolean mIsCameraConfigured = false; private Surface mCameraSurface = null; @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_main); this.mSurfaceView = (SurfaceView) findViewById(R.id.SurfaceViewPreview); this.mSurfaceHolder = this.mSurfaceView.getHolder(); this.mSurfaceHolder.addCallBACk(this); this.mCameraManager = (CameraManager) this.getSystemservice(Context.CAMERA_serviCE); try { mCameraIDsList = this.mCameraManager.getCameraIdList(); for (String id : mCameraIDsList) { Log.v(tag,"CameraID: " + id); } } catch (CameraAccessException E) { e.printStackTrace(); } mCameraStateCB = new CameraDevice.StateCallBACk() { @Override public void onOpened(CameraDevice camera) { Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); mCameraDevice = camera; mHandler.sendEmptymessage(MSG_CAMERA_OPENED); } @Override public void onDisconnected(CameraDevice camera) { Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); } @Override public void onError(CameraDevice camera,int error) { Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); } }; } @Override protected void onStart() { super.onStart(); //requesTing permission int permission@R_674_10943@k = ContextCompat.@R_674_10943@kSelfPermission(this,Manifest.permission.CAMERA); if (permission@R_674_10943@k != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowrequestPermissionRationale(this,Manifest.permission.CAMERA)) { } else { ActivityCompat.requestPermissions(this,MY_PERMISSIONS_requEST_CAMERA); Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); try { mCameraManager.openCamera(mCameraIDsList[1],mCameraStateCB,new Handler()); } catch (CameraAccessException E) { e.printStackTrace(); } } } @Override protected void onStop() { super.onStop(); try { if (mCaptureSession != null) { mCaptureSession.stopRepeaTing(); mCaptureSession.close(); mCaptureSession = null; } mIsCameraConfigured = false; } catch (final CameraAccessException E) { // Doesn't matter,cloising device anyway e.printStackTrace(); } catch (final IllegalStateException e2) { // Doesn't matter,cloising device anyway e2.printStackTrace(); } finally { if (mCameraDevice != null) { mCameraDevice.close(); mCameraDevice = null; mCaptureSession = null; } } } @Override public Boolean handlemessage(message msg) { switch (msg.what) { case MSG_CAMERA_OPENED: case MSG_SURFACE_READY: // if both surface is created and camera device is opened // - ready to set up preview and other things if (mSurfaceCreated && (mCameraDevice != null) && !mIsCameraConfigured) { configureCamera(); } break; } return true; } private void configureCamera() { // prepare list of surfaces to be used in capture requests List<Surface> sfl = new ArrayList<Surface>(); sfl.add(mCameraSurfacE); // surface for viewfinder preview // configure camera with all the surfaces to be ever used try { mCameraDevice.createCaptureSession(sfl,new CaptureSessionListener(),null); } catch (CameraAccessException E) { e.printStackTrace(); } mIsCameraConfigured = true; } @Override public void onrequestPermissionsResult(int requestCode,@NonNull int[] grantResults) { super.onrequestPermissionsResult(requestCode,grantResults); switch (requestCodE) { case MY_PERMISSIONS_requEST_CAMERA: if (ActivityCompat.@R_674_10943@kSelfPermission(this,Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) try { mCameraManager.openCamera(mCameraIDsList[1],new Handler()); } catch (CameraAccessException E) { e.printStackTrace(); } break; } } @Override public void surfaceCreated(SurfaceHolder holder) { mCameraSurface = holder.getSurface(); } @Override public void surfaceChanged(SurfaceHolder holder,int height) { mCameraSurface = holder.getSurface(); mSurfaceCreated = true; mHandler.sendEmptymessage(MSG_SURFACE_READY); } @Override public void surfaceDestroyed(SurfaceHolder holder) { mSurfaceCreated = false; } private class CaptureSessionListener extends CameraCaptureSession.StateCallBACk { @Override public void onConfigureFailed(final CameraCaptureSession session) { Log.d(tag,"CaptureSessionConfigure Failed"); } @Override public void onConfigured(final CameraCaptureSession session) { Log.d(tag,"CaptureSessionConfigure onConfigured"); mCaptureSession = session; try { Capturerequest.builder previewrequestBuilder = mCameraDevice .createCapturerequest(CameraDevice.TEMPLATE_PREVIEW); previewrequestBuilder.addTarget(mCameraSurfacE); mCaptureSession.setRepeaTingrequest(previewrequestBuilder.build(),null,null); } catch (CameraAccessException E) { Log.d(tag,"setTing up preview Failed"); e.printStackTrace(); } } } }
以上是大佬教程为你收集整理的android – 如何在SurfaceView上显示相机预览?全部内容,希望文章能够帮你解决android – 如何在SurfaceView上显示相机预览?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。