其他分享
首页 > 其他分享> > Camera2使用textureView支持

Camera2使用textureView支持

作者:互联网

SurfaceView 绘制会有独立窗口, TextureView 没有独立的窗口,可以像普通的 View 一样,更高效更方便

public class MainActivity extends AppCompatActivity {
    private TextureView textureView;
    private String TAG = "测试";
    private HandlerThread mThreadHandler;
    private Handler mHandler;

    private CaptureRequest.Builder mPreviewBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mThreadHandler = new HandlerThread("CAMERA2");
        mThreadHandler.start();
        mHandler = new Handler(mThreadHandler.getLooper());
        textureView = (TextureView) findViewById(R.id.tv);
        textureView.setSurfaceTextureListener(textureListener);
    }

    private TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
            Log.e(TAG, "可用");
            //CameraManaer 摄像头管理器,用于检测摄像头,打开系统摄像头
            CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
            try {
                String[] CameraIdList = cameraManager.getCameraIdList();//获取可用相机列表
                Log.e(TAG, "可用相机的个数是:" + CameraIdList.length);
                CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(CameraIdList[0]);//获取某个相机(摄像头特性)
                cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);//检查支持

                if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                cameraManager.openCamera(CameraIdList[0], mCameraDeviceStateCallback, mHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
                    Log.e(TAG,"改变");
                }

                @Override
                public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                    Log.e(TAG,"释放");
                    return false;
                }

                @Override
                public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
                    Log.e(TAG,"更新");
                }
            };

            //CameraDeviceandroid.hardware.Camera也就是Camera1的Camera
            private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
                @Override
                public void onOpened(CameraDevice camera) {
                    try {
                        startPreview(camera);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onDisconnected(CameraDevice camera) {

                }

                @Override
                public void one rror(CameraDevice camera, int error) {

                }
            };


            /**
             * @param camera
             * @throws CameraAccessException
             * 开始预览
             */
            private void startPreview(CameraDevice camera) throws CameraAccessException {
                SurfaceTexture texture = textureView.getSurfaceTexture();
                texture.setDefaultBufferSize(textureView.getWidth(), textureView.getHeight());
                Surface surface = new Surface(texture);
                try {
                    //CameraRequest表示一次捕获请求,用来对z照片的各种参数设置,比如对焦模式、曝光模式等。CameraRequest.Builder用来生成CameraRequest对象
                    mPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
                mPreviewBuilder.addTarget(surface);
                camera.createCaptureSession(Arrays.asList(surface), mSessionStateCallback, mHandler);
            }

            //CameraCaptureSession 这个对象控制摄像头的预览或者拍照
            //setRepeatingRequest()开启预览,capture()拍照
            //StateCallback监听CameraCaptureSession的创建
            private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(CameraCaptureSession session) {
                    Log.e(TAG,"相机创建成功!");
                    try {
                        session.capture(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);//拍照
                        session.setRepeatingRequest(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);//返回结果
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                        Log.e(TAG,"这里异常");
                    }
                }

                @Override
                public void onConfigureFailed(CameraCaptureSession session) {
                    Log.e(TAG,"相机创建失败!");
                }
            };


            //CameraCaptureSession.CaptureCallback监听拍照过程
            private CameraCaptureSession.CaptureCallback mSessionCaptureCallback = new CameraCaptureSession.CaptureCallback() {

                @Override
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                    Log.e(TAG,"这里接受到数据"+result.toString());
                }

                @Override
                public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult){

                }};
        }
<LinearLayout 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:id="@+id/line1"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextureView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

标签:Camera2,支持,public,textureView,TAG,private,Override,void,CameraCaptureSession
来源: https://www.cnblogs.com/Ocean123123/p/10980381.html