How to capture screenshot of camera preview with overlay?

前端 未结 1 1853
庸人自扰
庸人自扰 2020-12-18 17:09

I saw the following link for the above query but both the answers does not met the expectation.

[How to programmatically take a screenshot in Android?

Expect

相关标签:
1条回答
  • 2020-12-18 17:31

    I provided solution to a folk, please check this answer. He had trouble to get the result but copied code below is what I'm using in production since a year ago. please try it.

    The code captures image in SurfaceView which is given from Camera. You can overlay some views on it. They will be captured along with Camera preview.

    public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
        private static final String TAG = "CameraSurfaceView";
    
        private SurfaceHolder mSurfaceHolder;
        private Camera mCamera = null;
        private Bitmap mBitmap;
        private Context mContext;
        private Camera.Parameters mParameters;
        private byte[] byteArray;
        private List<Camera.Size> mSupportedPreviewSizes;
        private Camera.Size mPreviewSize;
    
        public CameraSurfaceView (Context context) {
            this(context, null);
        }
    
        public CameraSurfaceView (Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CameraSurfaceView (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mContext = context;
    
            try {
                mSurfaceHolder = getHolder();
                mSurfaceHolder.addCallback(this);
                mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void surfaceCreated(final SurfaceHolder surfaceHolder) {
            if (mCamera == null) {
                try {
                    mCamera = Camera.open();
                } catch (RuntimeException ignored) {
                }
            }
    
            try {
                if (mCamera != null) {
                    WindowManager winManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                    mCamera.setPreviewDisplay(mSurfaceHolder);
                }
            } catch (Exception e) {
                if (mCamera != null)
                    mCamera.release();
                mCamera = null;
            }
    
            if (mCamera == null) {
                    return;
            } else {
                mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                    @Override
                    public void onPreviewFrame(byte[] bytes, Camera camera) {
                        if (mParameters == null)
                        {
                            return;
                        }
                        byteArray = bytes;
                    }
                });
            }
    
            setWillNotDraw(false);
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
            try {
                mParameters = mCamera.getParameters();
    
                List<Size> cameraSize = mParameters.getSupportedPreviewSizes();
                mPreviewSize = cameraSize.get(0);
    
                for (Size s : cameraSize) {
                    if ((s.width * s.height) > (mPreviewSize.width * mPreviewSize.height)) {
                        mPreviewSize = s;
                    }
                }
    
                mParameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                mCamera.setParameters(mParameters);
                mCamera.startPreview();
    
            } catch (Exception e) {
                if (mCamera != null) {
                    mCamera.release();
                    mCamera = null;
                }
            }
        }
    
    
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            if (mCamera != null) {
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }
    
        public Bitmap getBitmap() {
            try {
                if (mParameters == null)
                    return null;
    
                if (mPreviewSize == null)
                    return null;
    
                int format = mParameters.getPreviewFormat();
                YuvImage yuvImage = new YuvImage(byteArray, format, mPreviewSize.width, mPreviewSize.height, null);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
                Rect rect = new Rect(0, 0, mPreviewSize.width, mPreviewSize.height);
    
                yuvImage.compressToJpeg(rect, 75, byteArrayOutputStream);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeByteArray(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size(), options);
    
                byteArrayOutputStream.flush();
                byteArrayOutputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
    
            return mBitmap;
        }
    
        public Camera getCamera() {
            return mCamera;
        }
    }
    
    0 讨论(0)
提交回复
热议问题