如何使用叠加捕获相机预览的屏幕截图?

时间:2015-10-20 15:05:42

标签: android android-camera

我在上面的查询中看到了以下链接,但这些答案都没有达到预期效果。

[How to programmatically take a screenshot in Android?

期望:使用叠加功能捕捉相机预览。

1 个答案:

答案 0 :(得分:2)

我为一个人提供了解决方案,请check this answer。他很难得到结果,但下面复制的代码是我一年前在制作中使用的。请试一试。

代码捕获SurfaceView中的Camera图片。您可以在其上叠加一些视图。它们将与相机预览一起被捕获。

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;
    }
}