南京城乡建设局网站,网站开发安装环境,外网网站有什么好的推荐,餐饮营销手段13种手段Android相机调用有原生的Camera和Camera2#xff0c;我觉得调用代码都太复杂了#xff0c;CameraX调用代码简洁很多。
说明文档#xff1a;https://developer.android.com/jetpack/androidx/releases/camera?hlzh-cn
现有查到的调用资料都不够新#xff0c;对于外接摄像…Android相机调用有原生的Camera和Camera2我觉得调用代码都太复杂了CameraX调用代码简洁很多。
说明文档https://developer.android.com/jetpack/androidx/releases/camera?hlzh-cn
现有查到的调用资料都不够新对于外接摄像头USB摄像头这类非前置也非后置摄像头的设备调用都说是没有实现。旧版本的库可能更多目标用户是基于手机的1.3.0-alpha03版本针对外接摄像头有增加配置项CameraSelector.LENS_FACING_EXTERNAL使用该配置项可以实现外接摄像头的调用。
0摄像头选择可用值 /** A camera on the devices that its lens facing is resolved. */public static final int LENS_FACING_UNKNOWN -1;/** A camera on the device facing the same direction as the devices screen. */public static final int LENS_FACING_FRONT 0;/** A camera on the device facing the opposite direction as the devices screen. */public static final int LENS_FACING_BACK 1;/*** An external camera that has no fixed facing relative to the devices screen.** pThe behavior of an external camera highly depends on the manufacturer. Currently its* treated similar to a front facing camera with little verification. So its considered* experimental and should be used with caution.*/ExperimentalLensFacingpublic static final int LENS_FACING_EXTERNAL 2;
1在AndroidManifest.xml添加权限 uses-permission android:nameandroid.permission.CAMERA /uses-feature android:nameandroid.hardware.camera.any /
2在settings.gradle或build.gradle添加maven repositories {google()mavenCentral()maven { url https://jitpack.io }}
3在build.gradle添加依赖库 //摄像头预览库implementation androidx.camera:camera-core:1.3.0-alpha04// CameraX Camera2 extensions[可选]拓展库可实现人像、HDR、夜间和美颜、滤镜但依赖于OEMimplementation androidx.camera:camera-camera2:1.3.0-alpha04// CameraX Lifecycle library[可选]避免手动在生命周期释放和销毁数据implementation androidx.camera:camera-lifecycle:1.3.0-alpha04// CameraX View class[可选]最佳实践最好用里面的PreviewView它会自行判断用SurfaceView还是TextureView来实现implementation androidx.camera:camera-view:1.3.0-alpha04
4开启预览代码 private ListenableFutureProcessCameraProvider cameraProviderFuture;private PreviewView previewView;private ProcessCameraProvider cameraProvider;ImageView picture null;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);picture (ImageView) findViewById(R.id.picture);previewViewfindViewById(R.id.previewView);//初始化//高版本系统动态权限申请if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) PackageManager.PERMISSION_DENIED) {if (Build.VERSION.SDK_INT Build.VERSION_CODES.M) {requestPermissions(new String[]{Manifest.permission.CAMERA,}, 11);}} else {//启动相机startCamera();}takePhoto.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View view) {takePhoto(picture);//取图识别}});}Overridepublic void onRequestPermissionsResult(int requestCode, NonNull String[] permissions, NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode11){//获取权限后开启摄像头//启动相机startCamera();}}private void startCamera() {// 请求 CameraProvidercameraProviderFuture ProcessCameraProvider.getInstance(this);//检查 CameraProvider 可用性验证它能否在视图创建后成功初始化cameraProviderFuture.addListener(() - {try {ProcessCameraProvider cameraProvider cameraProviderFuture.get();bindPreview(cameraProvider);} catch (ExecutionException | InterruptedException e) {// No errors need to be handled for this Future.// This should never be reached.}}, ContextCompat.getMainExecutor(this));}//选择相机并绑定生命周期和用例private void bindPreview(NonNull ProcessCameraProvider cp) {this.cameraProvidercp;Preview preview new Preview.Builder().build();SuppressLint(UnsafeOptInUsageError)CameraSelector cameraSelector new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK)//CameraSelector.LENS_FACING_EXTERNAL.build();preview.setSurfaceProvider(previewView.getSurfaceProvider());cameraProvider.unbindAll();//解绑组件cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview);}//拍照这里偷懒了直接取了预览控件的图片需要拍照的再去看看官方文档吧public void takePhoto(View view){Log.e(OCR, takePhoto);Bitmap bitmap previewView.getBitmap();view.setBackground(new BitmapDrawable(getApplicationContext().getResources(),bitmap)); //show picture}Overrideprotected void onDestroy() {super.onDestroy();cameraProvider.unbindAll();}
5界面布局
?xml version1.0 encodingutf-8?
androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margin5dpandroid:orientationverticalandroidx.camera.view.PreviewViewandroid:idid/previewViewandroid:layout_width300dpandroid:layout_height300dpandroid:layout_gravitycenter/Buttonandroid:idid/take_photoandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margin5dpandroid:text取图/ImageViewandroid:idid/pictureandroid:layout_width300dpandroid:layout_height300dpandroid:layout_gravitycenter//LinearLayout/androidx.coordinatorlayout.widget.CoordinatorLayout
6测试效果文字识别部分请忽略 s 7库的调用版本是比较新的建议JDK版本不要太低我使用的是16.0.2 新人入行经验分享如有所误欢迎指出~
版权归属深圳市琪智科技有限公司-花花