美文网首页人脸识别
android Camera不显示预览获取帧数据

android Camera不显示预览获取帧数据

作者: MacLi | 来源:发表于2019-05-23 16:26 被阅读0次
    public class MainActivity extends AppCompatActivity {
    
        private int mCameraIndex = Camera.CameraInfo.CAMERA_FACING_FRONT;
        private Camera mCamera;
        private static final int SIZE_WIDTH = 960;
        private static final int SIZE_HEIGHT = 540;
        // 设置缓存帧数据容器,避免重复创建
        private byte[] mPreviewData = new byte[SIZE_WIDTH * SIZE_HEIGHT * 3 / 2];
        private ImageView mImageView;
        private boolean isShow = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            mImageView = findViewById(R.id.imageview);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            initCameara();
            setCallback();
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            closeCamera();
        }
    
        public void clickButton(View view) {
            isShow = true;
        }
    
        /**
         * Camera初始化
         */
        private void initCameara() {
            mCamera = Camera.open(mCameraIndex);
            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewFormat(ImageFormat.NV21);
            p.setPreviewSize(SIZE_WIDTH, SIZE_HEIGHT);
            p.setPictureFormat(ImageFormat.NV21);
            p.setPictureSize(SIZE_WIDTH, SIZE_HEIGHT);
            p.setZoom(0);
            p.setRotation(0);
            mCamera.setParameters(p);
        }
    
        /**
         * 开始监听回调,设置预览
         */
        private void setCallback() {
            try {
                // 主要是surfaceTexture获取预览数据,但不显示
                SurfaceTexture surfaceTexture = new SurfaceTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES);
                mCamera.setPreviewTexture(surfaceTexture);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 设置 mCamera.addCallbackBuffer(mPreviewData) 后才会回调,旨在每处理完一帧数据回调一次
            mCamera.setPreviewCallbackWithBuffer(mPreviewCallback);
            mCamera.addCallbackBuffer(mPreviewData);
            mCamera.startPreview();
        }
    
        /**
         * 帧数据监听实现
         */
        private Camera.PreviewCallback mPreviewCallback = new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                // 在此处处理当前帧数据,并设置下一帧回调
                mCamera.addCallbackBuffer(mPreviewData);
                if (isShow) {
                    showPic(mPreviewData);
                    isShow = false;
                }
            }
        };
    
        /**
         * 显示图片
         */
        private void showPic(byte[] data){
            try {
                YuvImage  image = new YuvImage(data, ImageFormat.NV21,SIZE_WIDTH, SIZE_HEIGHT, null);
                if (image != null) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    image.compressToJpeg(new Rect(0, 0, SIZE_WIDTH, SIZE_HEIGHT), 100, stream);
                    Bitmap bm = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
                    stream.close();
                    bm = Util.convertBmp(bm);
                    mImageView.setImageBitmap(bm);
                }
            } catch (Exception e) {
            }
        }
    
        /**
         * 关闭相机
         */
        private void closeCamera() {
            mCamera.stopPreview();
            mCamera.setPreviewCallbackWithBuffer(null);
            mCamera.release();
            mCamera = null;
        }
    }
    

    相关文章

      网友评论

        本文标题:android Camera不显示预览获取帧数据

        本文链接:https://www.haomeiwen.com/subject/ztjbzqtx.html