網(wǎng)站建設怎樣上傳程序企業(yè)網(wǎng)站營銷的實現(xiàn)方式
Android相機調用有原生的Camera和Camera2,我覺得調用代碼都太復雜了,CameraX調用代碼簡潔很多。
說明文檔:https://developer.android.com/jetpack/androidx/releases/camera?hl=zh-cn
現(xiàn)有查到的調用資料都不夠新,對于外接攝像頭(USB攝像頭)這類非前置也非后置攝像頭的設備調用,都說是沒有實現(xiàn)。舊版本的庫可能更多目標用戶是基于手機的,1.3.0-alpha03版本針對外接攝像頭有增加配置項(CameraSelector.LENS_FACING_EXTERNAL),使用該配置項可以實現(xiàn)外接攝像頭的調用。
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 device's screen. */public static final int LENS_FACING_FRONT = 0;/** A camera on the device facing the opposite direction as the device's screen. */public static final int LENS_FACING_BACK = 1;/*** An external camera that has no fixed facing relative to the device's screen.** <p>The behavior of an external camera highly depends on the manufacturer. Currently it's* treated similar to a front facing camera with little verification. So it's considered* experimental and should be used with caution.*/@ExperimentalLensFacingpublic static final int LENS_FACING_EXTERNAL = 2;
1,在AndroidManifest.xml添加權限
<uses-permission android:name="android.permission.CAMERA" /><uses-feature android:name="android.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[可選]拓展庫可實現(xiàn)人像、HDR、夜間和美顏、濾鏡但依賴于OEMimplementation "androidx.camera:camera-camera2:1.3.0-alpha04"// CameraX Lifecycle library[可選]避免手動在生命周期釋放和銷毀數(shù)據(jù)implementation "androidx.camera:camera-lifecycle:1.3.0-alpha04"// CameraX View class[可選]最佳實踐,最好用里面的PreviewView,它會自行判斷用SurfaceView還是TextureView來實現(xiàn)implementation 'androidx.camera:camera-view:1.3.0-alpha04'
4,開啟預覽代碼
private ListenableFuture<ProcessCameraProvider> 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);previewView=findViewById(R.id.previewView);//初始化//高版本系統(tǒng)動態(tài)權限申請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 (requestCode==11){//獲取權限后,開啟攝像頭//啟動相機startCamera();}}private void startCamera() {// 請求 CameraProvidercameraProviderFuture = ProcessCameraProvider.getInstance(this);//檢查 CameraProvider 可用性,驗證它能否在視圖創(chuàng)建后成功初始化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}@Overrideprotected void onDestroy() {super.onDestroy();cameraProvider.unbindAll();}
5,界面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="5dp"android:orientation="vertical"><androidx.camera.view.PreviewViewandroid:id="@+id/previewView"android:layout_width="300dp"android:layout_height="300dp"android:layout_gravity="center"/><Buttonandroid:id="@+id/take_photo"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="5dp"android:text="取圖"/><ImageViewandroid:id="@+id/picture"android:layout_width="300dp"android:layout_height="300dp"android:layout_gravity="center"/></LinearLayout></androidx.coordinatorlayout.widget.CoordinatorLayout>
6,測試效果(文字識別部分請忽略)
s
7,庫的調用版本是比較新的,建議JDK版本不要太低,我使用的是16.0.2
新人入行,經(jīng)驗分享,如有所誤,歡迎指出~
版權歸屬:深圳市琪智科技有限公司-花花