How To Capture Screenshot Of Camera Preview With Overlay?
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? Expectation: Capture camera p
Solution 1:
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.
publicclassCameraSurfaceViewextendsSurfaceViewimplementsSurfaceHolder.Callback {
privatestaticfinalStringTAG="CameraSurfaceView";
private SurfaceHolder mSurfaceHolder;
privateCameramCamera=null;
private Bitmap mBitmap;
private Context mContext;
private Camera.Parameters mParameters;
privatebyte[] byteArray;
private List<Camera.Size> mSupportedPreviewSizes;
private Camera.Size mPreviewSize;
publicCameraSurfaceView(Context context) {
this(context, null);
}
publicCameraSurfaceView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
publicCameraSurfaceView(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();
}
}
@OverridepublicvoidsurfaceCreated(final SurfaceHolder surfaceHolder) {
if (mCamera == null) {
try {
mCamera = Camera.open();
} catch (RuntimeException ignored) {
}
}
try {
if (mCamera != null) {
WindowManagerwinManager= (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(newCamera.PreviewCallback() {
@OverridepublicvoidonPreviewFrame(byte[] bytes, Camera camera) {
if (mParameters == null)
{
return;
}
byteArray = bytes;
}
});
}
setWillNotDraw(false);
}
@OverridepublicvoidsurfaceChanged(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;
}
}
}
@OverridepublicvoidsurfaceDestroyed(SurfaceHolder surfaceHolder) {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public Bitmap getBitmap() {
try {
if (mParameters == null)
returnnull;
if (mPreviewSize == null)
returnnull;
intformat= mParameters.getPreviewFormat();
YuvImageyuvImage=newYuvImage(byteArray, format, mPreviewSize.width, mPreviewSize.height, null);
ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();
Rectrect=newRect(0, 0, mPreviewSize.width, mPreviewSize.height);
yuvImage.compressToJpeg(rect, 75, byteArrayOutputStream);
BitmapFactory.Optionsoptions=newBitmapFactory.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;
}
}
Post a Comment for "How To Capture Screenshot Of Camera Preview With Overlay?"