Android相机调用-CameraX【外接摄像头】【USB摄像头】
Android相机调用有原生的Camera和Camera2,我觉得调用代码都太复杂了,CameraX调用代码简洁很多。
说明文档:https://developer.android.com/jetpack/androidx/releases/camera?hl=zh-cn
现有查到的调用资料都不够新,对于外接摄像头(USB摄像头)这类非前置也非后置摄像头的设备调用,都说是没有实现。旧版本的库可能更多目标用户是基于手机的,1.3.0-alpha03版本针对外接摄像头有增加配置项(CameraSelector.LENS_FACING_EXTERNAL),使用该配置项可以实现外接摄像头的调用。
0,摄像头选择可用值
public static final int LENS_FACING_UNKNOWN = -1; public static final int LENS_FACING_FRONT = 0; public static final int LENS_FACING_BACK = 1; @ExperimentalLensFacing public static final int LENS_FACING_EXTERNAL = 2;
1,在AndroidManifest.xml添加权限
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、夜间和美颜、滤镜但依赖于OEM implementation "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 ListenableFuture cameraProviderFuture; private PreviewView previewView; private ProcessCameraProvider cameraProvider; ImageView picture = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); picture = (ImageView) findViewById(R.id.picture); previewView=findViewById(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() { @Override public void onClick(View view) { takePhoto(picture);//取图识别 } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode==11){//获取权限后,开启摄像头 //启动相机 startCamera(); } } private void startCamera() { // 请求 CameraProvider cameraProviderFuture = 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.cameraProvider=cp; 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 } @Override protected void onDestroy() { super.onDestroy(); cameraProvider.unbindAll(); }
5,界面布局
6,测试效果(文字识别部分请忽略)
s
7,库的调用版本是比较新的,建议JDK版本不要太低,我使用的是16.0.2
新人入行,经验分享,如有所误,欢迎指出~
版权归属:深圳市琪智科技有限公司-花花
来源地址:https://blog.csdn.net/lsh670660992/article/details/132738080
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341