淘宝客网站需要备案吗,中山有哪些网站建立公司,教育类网站前置审批,如何创建自己网站如果要在安卓应用中操作相机#xff0c;有三个库可以选#xff1a;
Camera#xff08;已废弃#xff09;#xff1a;Camera是安卓最早的包#xff0c;目前已废弃#xff0c;在Android 5.0#xff08;API 级别 21#xff09;的设备上操作相机可以选择该包#xff0c;…如果要在安卓应用中操作相机有三个库可以选
Camera已废弃Camera是安卓最早的包目前已废弃在Android 5.0API 级别 21的设备上操作相机可以选择该包保证兼容性Camera2Camera2在Android 5.0API 级别 21开始提供用于替代Camera相比Camera在性能、灵活性等方面有优势但是使用起来更复杂CameraXCameraX是对Camera2的封装是Jetpack的一个库支持Android 5.0API 级别 21及更高版本。CameraX降低了操作相机的难度大多数情况下使用CameraX已足以完成需求建议使用CameraX。
注Jetpack是一个由多个库组成的套件可帮助开发者遵循最佳做法、减少样板代码并编写可在各种Android版本和设备中一致运行的代码让开发者可将精力集中于真正重要的编码工作。
谷歌的文档只提供了Kotlin的代码示例根据Kotlin的代码示例和文档本文提供Java的实现。
1. CameraX常见用例
您可以使用CameraX借助名为“用例”的抽象概念与设备的相机进行交互。提供的用例如下
预览Preview在屏幕上查看相机画面图片分析ImageAnalysis逐帧处理相机捕获到的每一帧画面图片拍摄ImageCapture拍照视频拍摄VideoCapture拍视频和音频。
CameraX允许同时使用Preview、VideoCapture、ImageAnalysis和ImageCapture各一个实例。此外
每个用例都可以单独使用。例如应用可以在不使用预览的情况下录制视频启用扩展后只能保证能够使用ImageCapture和Preview的组合。根据OEM实现情况可能无法同时添加ImageAnalysis无法为VideoCapture用例启用扩展。如需了解详情请参阅扩展参考文档对于某些相机而言在较低分辨率模式下可以支持的组合在较高的分辨率下将无法支持这具体取决于相机的功能在相机硬件级别为FULL或更低的设备上组合使用Preview、VideoCapture和ImageCapture或ImageAnalysis可能会迫使CameraX为Preview和VideoCapture复制相机的PRIV数据流。这种重复称为数据流共享可让您同时使用这些功能但代价是增加了处理需求。因此您可能会遇到略长的延迟时间和缩短的电池续航时间。
2. 代码实现
在build.gradle中添加相关依赖
// camerax
def camerax_version 1.3.0
implementation androidx.camera:camera-core:${camerax_version}
implementation androidx.camera:camera-camera2:${camerax_version}
implementation androidx.camera:camera-lifecycle:${camerax_version}
implementation androidx.camera:camera-view:${camerax_version}
implementation androidx.camera:camera-video:${camerax_version}
// 扩展本文代码未使用
// implementation androidx.camera:camera-extensions:${camerax_version}在AndroidManifest.xml添加相关权限
uses-permission android:nameandroid.permission.CAMERA/
uses-permission android:nameandroid.permission.RECORD_AUDIO /
uses-permission android:nameandroid.permission.WRITE_EXTERNAL_STORAGE/添加布局文件activity_camerax_preview.xml
?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:fitsSystemWindowstrueandroidx.camera.view.PreviewViewandroid:idid/camerax_preview_viewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parent /RelativeLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_alignParentBottomtrueLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationverticalLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:gravitycenterButtonandroid:idid/camerax_flashandroid:layout_width110dpandroid:layout_height70dpandroid:layout_marginStart10dpandroid:layout_marginTop5dpandroid:layout_marginEnd10dpandroid:layout_marginBottom5dpandroid:text开灯 /Buttonandroid:idid/camerax_switch_cameraandroid:layout_width110dpandroid:layout_height70dpandroid:layout_marginStart10dpandroid:layout_marginTop5dpandroid:layout_marginEnd10dpandroid:layout_marginBottom5dpandroid:text切换摄像头 //LinearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:gravitycenterButtonandroid:idid/camerax_image_capture_buttonandroid:layout_width110dpandroid:layout_height70dpandroid:layout_marginStart10dpandroid:layout_marginTop5dpandroid:layout_marginEnd10dpandroid:layout_marginBottom5dpandroid:text拍照 /Buttonandroid:idid/camerax_video_capture_buttonandroid:layout_width110dpandroid:layout_height70dpandroid:layout_marginStart10dpandroid:layout_marginTop5dpandroid:layout_marginEnd10dpandroid:layout_marginBottom5dpandroid:text开始录制 /Buttonandroid:idid/camerax_video_capture_pause_buttonandroid:layout_width110dpandroid:layout_height70dpandroid:layout_marginStart10dpandroid:layout_marginTop5dpandroid:layout_marginEnd10dpandroid:layout_marginBottom5dpandroid:text暂停录制android:visibilitygone //LinearLayout/LinearLayout/RelativeLayout
/RelativeLayoutactivity类
package org.tao.hetools.activities;import android.Manifest;
import android.content.ContentValues;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Size;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.Camera;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.Preview;
import androidx.camera.core.TorchState;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.video.MediaStoreOutputOptions;
import androidx.camera.video.Quality;
import androidx.camera.video.QualitySelector;
import androidx.camera.video.Recorder;
import androidx.camera.video.Recording;
import androidx.camera.video.VideoCapture;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;import com.google.common.util.concurrent.ListenableFuture;import org.tao.hetools.R;
import org.tao.hetools.utils.PermissionUtils;import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;public class CameraxPreviewActivity extends ComponentActivity {private static final String TAG CameraxPreviewActivity;private static final SimpleDateFormat SIMPLE_DATE_FORMAT new SimpleDateFormat(yyyyMMddHHmmssSSS);private static final Size SIZE new Size(1080, 1920);private static final ListCameraSelector CAMERAS Arrays.asList(CameraSelector.DEFAULT_BACK_CAMERA,CameraSelector.DEFAULT_FRONT_CAMERA);private static final String START_RECORD 开始录制;private static final String STOP_RECORD 结束录制;private static final String PAUSE_RECORD 暂停录制;private static final String RESUME_RECORD 恢复录制;private PreviewView previewView;private Executor executor;private Preview preview;private ImageAnalysis imageAnalysis;private ImageCapture imageCapture;private VideoCaptureRecorder videoCapture;private Recording videoRecording;private ProcessCameraProvider cameraProvider;private Camera camera;private boolean isRecordingStart false;private boolean isRecordingPause false;private int cameraIndex 0;private Button flashButton;private Button pauseResumeVideoButton;private Button startShopVideoButton;private Button switchCameraButton;Overrideprotected void onCreate(Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.activity_camerax_preview);PermissionUtils.checkPermission(this, Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE);previewView findViewById(R.id.camerax_preview_view);executor ContextCompat.getMainExecutor(this);initCamerax();initButton();}private void initCamerax() {// 可以将相机生命周期绑定到activity从而免去打开、关闭相机的任务ListenableFutureProcessCameraProvider cameraProviderListenableFuture ProcessCameraProvider.getInstance(this);cameraProviderListenableFuture.addListener(() - {try {cameraProvider cameraProviderListenableFuture.get();// 预览preview new Preview.Builder().build();preview.setSurfaceProvider(previewView.getSurfaceProvider());// 图片分析imageAnalysis new ImageAnalysis.Builder()// 设置输出图片格式.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888)// 非阻塞模式只取最新的图片。若需要每帧图片都处理使用阻塞模式 ImageAnalysis.STRATEGY_BLOCK_PRODUCER.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST).setTargetResolution(SIZE).build();imageAnalysis.setAnalyzer(executor, (imageProxy) - {// close后才认为处理完成当前帧所以放到try-with-resources语句中try (imageProxy) {// 在此处处理图片如对图片进行人脸、条码识别等Log.i(TAG, 接收到一帧图片 SIMPLE_DATE_FORMAT.format(new Date()));} catch (Exception exception) {Log.w(TAG, exception.getMessage());}});// 拍照imageCapture new ImageCapture.Builder().setFlashMode(ImageCapture.FLASH_MODE_AUTO).setTargetResolution(SIZE).build();// 录制Recorder recorder new Recorder.Builder().setQualitySelector(QualitySelector.from(Quality.FHD)).build();videoCapture VideoCapture.withOutput(recorder);cameraProvider.unbindAll();camera cameraProvider.bindToLifecycle(this, CAMERAS.get(cameraIndex), preview,imageCapture, videoCapture);} catch (ExecutionException e) {throw new RuntimeException(e);} catch (InterruptedException e) {throw new RuntimeException(e);}}, executor);}private void initButton() {initFlashButton();initSwitchButton();initImageCaptureButton();initVideoCaptureButtons();}/*** 视频操作按钮*/private void initVideoCaptureButtons() {startShopVideoButton findViewById(R.id.camerax_video_capture_button);pauseResumeVideoButton findViewById(R.id.camerax_video_capture_pause_button);// 开始/停止录制视频startShopVideoButton.setOnClickListener(view - {if (videoCapture null || executor null) {return;}if (isRecordingStart) {videoRecording.stop();videoRecording null;isRecordingStart false;startShopVideoButton.setText(START_RECORD);// 视频录制停止后不显示暂停/恢复录制按钮pauseResumeVideoButton.setVisibility(View.GONE);// 录制视频完成后显示切换摄像头按钮switchCameraButton.setVisibility(View.VISIBLE);return;}PermissionUtils.checkPermission(this, Manifest.permission.RECORD_AUDIO);videoRecording videoCapture.getOutput().prepareRecording(this, getMediaStoreOutputOptions()).withAudioEnabled().start(executor, videoRecordEvent - {});isRecordingStart true;startShopVideoButton.setText(STOP_RECORD);pauseResumeVideoButton.setText(PAUSE_RECORD);// 视频录制开始后显示暂停/恢复录制按钮pauseResumeVideoButton.setVisibility(View.VISIBLE);// 录制视频期间不允许切换摄像头切换摄像头会终止录制switchCameraButton.setVisibility(View.GONE);});// 暂停/恢复录制视频pauseResumeVideoButton.setOnClickListener(view - {if (videoCapture null || executor null || videoRecording null || !isRecordingStart) {return;}if (isRecordingPause) {isRecordingPause false;videoRecording.resume();pauseResumeVideoButton.setText(PAUSE_RECORD);} else {videoRecording.pause();isRecordingPause true;pauseResumeVideoButton.setText(RESUME_RECORD);}});}/*** 拍照按钮*/private void initImageCaptureButton() {findViewById(R.id.camerax_image_capture_button).setOnClickListener(view - {if (imageCapture null || executor null) {return;}imageCapture.takePicture(getImageOutputFileOptions(), executor,new ImageCapture.OnImageSavedCallback() {Overridepublic void onImageSaved(NonNull ImageCapture.OutputFileResults outputFileResults) {Log.i(TAG, save picture success);Toast.makeText(CameraxPreviewActivity.this, success, Toast.LENGTH_SHORT).show();}Overridepublic void onError(NonNull ImageCaptureException exception) {Log.w(TAG, error exception.getMessage());Toast.makeText(CameraxPreviewActivity.this, fail, Toast.LENGTH_SHORT).show();}});});}/*** 切换摄像头按钮*/private void initSwitchButton() {switchCameraButton findViewById(R.id.camerax_switch_camera);switchCameraButton.setOnClickListener((view - {if (cameraProvider null) {return;}if (cameraIndex CAMERAS.size()) {cameraIndex 0;}cameraProvider.unbindAll();cameraProvider.bindToLifecycle(this, CAMERAS.get(cameraIndex), preview, imageAnalysis,imageCapture, videoCapture);resetButtonStatus(cameraIndex);}));}/*** 闪光灯按钮*/private void initFlashButton() {flashButton findViewById(R.id.camerax_flash);flashButton.setOnClickListener(view - {if (camera null) {return;}boolean isTorchOff camera.getCameraInfo().getTorchState().getValue() TorchState.OFF;camera.getCameraControl().enableTorch(isTorchOff);flashButton.setText(camera.getCameraInfo().getTorchState().getValue() TorchState.ON ? 关灯 : 开灯);});}/*** 重设按钮状态*/private void resetButtonStatus(int cameraIndex) {flashButton.setText(开灯);flashButton.setVisibility(cameraIndex 0 ? View.VISIBLE : View.GONE);startShopVideoButton.setText(START_RECORD);pauseResumeVideoButton.setText(PAUSE_RECORD);pauseResumeVideoButton.setVisibility(View.GONE);}private ImageCapture.OutputFileOptions getImageOutputFileOptions() {return new ImageCapture.OutputFileOptions.Builder(getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI,getContentValues(image/png)).build();}private MediaStoreOutputOptions getMediaStoreOutputOptions() {return new MediaStoreOutputOptions.Builder(getContentResolver(),MediaStore.Video.Media.EXTERNAL_CONTENT_URI).setContentValues(getContentValues(video/mp4)).build();}private ContentValues getContentValues(String mimeType) {String name SIMPLE_DATE_FORMAT.format(new Date());ContentValues contentValues new ContentValues();contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);if (Build.VERSION.SDK_INT Build.VERSION_CODES.P) {contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, Pictures/HeTools);}return contentValues;}
}参考文章
开始使用Android相机 文章转载自: http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.kwnnx.cn.gov.cn.kwnnx.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.fprll.cn.gov.cn.fprll.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.haolipu.com.gov.cn.haolipu.com