日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android實現極簡打開攝像頭_Android

作者:噠噠呵 ? 更新時間: 2022-06-01 編程語言

很多時候忘記Android攝像頭如何打開,查看google文檔的話,發現太復雜(只是單純的想打開攝像頭而已,不想添加那么多設置,添加那么功能),很多博客也是對官方文檔的小修小改,連方法名都一樣,因此,我決定完成Android相機最簡單的打開攝像頭(僅僅打開)。很久沒用忘掉的話,打開鏈接復制粘貼一下就完事了。

AndroidManifest.xml設置CAMERA權限后,在代碼中還要設置權限檢查,但是因為我連權限檢查都懶得加了,裝好后直接在手機系統里手動允許權限。

Camera1(已廢棄):

xml中使用SurfaceView作為預覽View

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
? ? 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">

? ? <SurfaceView
? ? ? ? android:id="@+id/surfaceView"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? app:layout_constraintLeft_toLeftOf="parent"
? ? ? ? app:layout_constraintRight_toRightOf="parent"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity:

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{

? ? private SurfaceHolder holder;
? ? private Camera camera;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? requestWindowFeature(Window.FEATURE_NO_TITLE);
? ? ? ? getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_FULLSCREEN);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? SurfaceView surfaceView = findViewById(R.id.surfaceView);
? ? ? ? holder = surfaceView.getHolder();
? ? ? ? holder.addCallback(this);
? ? }

? ? @Override
? ? public void surfaceCreated(SurfaceHolder holder) {
? ? ? ? if(camera == null){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
? ? ? ? ? ? ? ? camera.setPreviewDisplay(holder);
? ? ? ? ? ? ? ? camera.startPreview();
? ? ? ? ? ? ? ? Camera.Parameters parameters = camera.getParameters();
? ? ? ? ? ? ? ? parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
? ? ? ? ? ? ? ? parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
? ? ? ? ? ? ? ? camera.setParameters(parameters);
? ? ? ? ? ? ? ? camera.setDisplayOrientation(90);
? ? ? ? ? ? }catch (Exception e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

這樣就完成了最簡單的打開攝像頭并在手機中出現畫面。(代碼里去掉2個接口中未實現的方法)

Camera2

Android 5.0(API 21)以后,谷歌就決定廢棄原有的Camera API改用Camera2 API,因為功能更強大

xml使用TextureView作為預覽(其實SurfaceView也行,官方的Demo是用TextureView的一個子類):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
? ? 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">

? ? <TextureView
? ? ? ? android:id="@+id/surfaceView"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="0dp"
? ? ? ? app:layout_constraintBottom_toBottomOf="parent"
? ? ? ? app:layout_constraintLeft_toLeftOf="parent"
? ? ? ? app:layout_constraintRight_toRightOf="parent"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity:

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

? ? private TextureView textureView;

? ? private CaptureRequest.Builder builder;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? textureView = findViewById(R.id.surfaceView);
? ? ? ? textureView.setSurfaceTextureListener(this);
? ? }

? ? @Override
? ? public void onSurfaceTextureAvailable(final SurfaceTexture surface, int width, int height) {
? ? ? ? CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
? ? ? ? if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
? ? ? ? ? ? ? ? != PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? manager.openCamera("0", new CameraDevice.StateCallback() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onOpened(@NonNull CameraDevice camera) {
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
? ? ? ? ? ? ? ? ? ? surfaceTexture.setDefaultBufferSize(1440,1080);
? ? ? ? ? ? ? ? ? ? Surface surface = new Surface(surfaceTexture);
? ? ? ? ? ? ? ? ? ? builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
? ? ? ? ? ? ? ? ? ? builder.addTarget(surface);

? ? ? ? ? ? ? ? ? ? camera.createCaptureSession(Arrays.asList(surface),
? ? ? ? ? ? ? ? ? ? ? ? ? ? new CameraCaptureSession.StateCallback() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onConfigured(@NonNull CameraCaptureSession session) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? session.setRepeatingRequest(builder.build(), null, null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (CameraAccessException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onConfigureFailed(@NonNull CameraCaptureSession session) {

? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }, null);
? ? ? ? ? ? ? ? ? ? } catch (CameraAccessException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onDisconnected(@NonNull CameraDevice camera) {

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onError(@NonNull CameraDevice camera, int error) {

? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, null);
? ? ? ? } catch (CameraAccessException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}

這樣就成功使用Camera2的API打開并預覽了(代碼里去掉3個接口中未實現的方法)

原文鏈接:https://blog.csdn.net/weixin_45253393/article/details/105323765

欄目分類
最近更新