My camera app displays a camera preview on the screen and also processes it in the background. Here is the relevant code, condensed as much as possible (e.g. no error handling or field declarations shown):
public final class CameraView extends SurfaceView implements
SurfaceHolder.Callback, Runnable, PreviewCallback {
public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
void openCamera() {
// Called from parent activity after setting content view to CameraView
mCamera = Camera.open();
mCamera.setPreviewCallbackWithBuffer(this);
}
public void surfaceCreated(SurfaceHolder holder) {
new Thread(this).start();
// Set CameraView to the optimal camera preview size
final Camera.Parameters params = mCamera.getParameters();
final List<Camera.Size> sizes = params.getSupportedPreviewSizes();
final int screenWidth = ((View) getParent()).getWidth();
int minDiff = Integer.MAX_VALUE;
Camera.Size bestSize = null;
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// Find the camera preview width that best matches the
// width of the surface.
for (Camera.Size size : sizes) {
final int diff = Math.abs(size.width - screenWidth);
if (diff < minDiff) {
minDiff = diff;
bestSize = size;
}
}
} else {
// Find the camera preview HEIGHT that best matches the
// width of the surface, since the camera preview is rotated.
mCamera.setDisplayOrientation(90);
for (Camera.Size size : sizes) {
final int diff = Math.abs(size.height - screenWidth);
if (Math.abs(size.height - screenWidth) < minDiff) {
minDiff = diff;
bestSize = size;
}
}
}
final int previewWidth = bestSize.width;
final int previewHeight = bestSize.height;
ViewGroup.LayoutParams layoutParams = getLayoutParams();
layoutParams.height = previewHeight;
layoutParams.width = previewWidth;
setLayoutParams(layoutParams);
params.setPreviewFormat(ImageFormat.NV21);
mCamera.setParameters(params);
int size = previewWidth * previewHeight *
ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
mBuffer = new byte[size];
mCamera.addCallbackBuffer(mBuffer);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
public void onPreviewFrame(byte[] data, Camera camera) {
CameraView.this.notify();
}
public void run() {
mThreadRun = true;
while (mThreadRun) {
synchronized (this) {
this.wait();
processFrame(mBuffer); // convert to RGB and rotate - not shown
}
// Request a new frame from the camera by putting
// the buffer back into the queue
mCamera.addCallbackBuffer(mBuffer);
}
mHolder.removeCallback(this);
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
public void surfaceDestroyed(SurfaceHolder holder) {
mThreadRun = false;
}
}
On all devices, the camera preview displays properly, and on most (emulator, Samsung Galaxy S3, etc.) the data stored in mBuffer is also correct (after NV21 to RGB conversion and rotation, of course). However, a number of devices do not supply the correct data in onPreviewFrame. I'm sure that the data is being converted to RGB correctly after it's received, so the problem appears to be in the raw data supplied to mBuffer. I've noticed this bug report relating to the YV12 (alias YUV420p) camera preview format, but I'm using the old default, NV21 (alias YUV420sp), which must be supported according to the compatibility standard (see 7.5.3.2, bottom of page 29).
For example, for this scene (shown here in Camera Preview on the Samsung Galaxy Tab 2):
the data passed to mBuffer on the Tab 2 looks like:
and on the Motorola Droid 4 looks like:
What is the correct way to get Android camera preview data across all devices?
Edit: for processFrame(), I used OpenCV to convert to RGB and rotate. See this answer and this answer.
The only problem was that I didn't set the preview width and height:
params.setPreviewSize(previewWidth, previewHeight);
mCamera.setParameters(params);
This meant that the height and width I allocated for the array (proportional to previewWidth * previewHeight) tended to be a lot larger than the size of the actual data being returned (proportional to the default preview width and preview height). On some phones, the default was the same size as previewWidth and previewHeight, so there was no issue.
You can also try this
public void takeSnapPhoto() {
camera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int format = parameters.getPreviewFormat();
//YUV formats require more conversion
if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// Get the YuV image
YuvImage yuv_image = new YuvImage(data, format, w, h, null);
// Convert YuV to Jpeg
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
yuv_image.compressToJpeg(rect, 100, output_stream);
byte[] byt = output_stream.toByteArray();
FileOutputStream outStream = null;
try {
// Write to SD Card
File file = createFileInSDCard(FOLDER_PATH, "Image_"+System.currentTimeMillis()+".jpg");
//Uri uriSavedImage = Uri.fromFile(file);
outStream = new FileOutputStream(file);
outStream.write(byt);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
});}
From working on Barcode Scanner, which relies heavily on preview data, I feel like I've seen every bug under the sun. My suggestion is simply to not call setPreviewFormat() and let it use the default. The default is what you want here fortunately. There seem to be fewer devices that fail to get the default right, than device that balls up the call to setPreviewFormat(). Try that at least, may or may not be it.
Related
I am writing an Android plugin that will take data from the camera preview and send it to Unity. (For various reasons, I am not able to use the Unity WebCamTexture) I am able to get the camera preview data and sent it to Unity, however, the image is always rotated 90 degrees. My Unity app is set to always be in portrait mode.
On my Pixel XL, the front and back images are rotated in opposite directions. Here is a photo of my app when using the front and back facing cameras.
I have created the function AdjustOrientation in NativeCamera.java in an attempt to fix the orientation, but calling it does not seem to to have any effect.
I have found links to places that say to add in code similar to what is in the AdjustOrientation function to fix the problem, but none of them have solved the problem.
Here is a link I have investigated that did not solve my problem.
Android - Camera preview is sideways
I have tried different variations of
mCamera.setDisplayOrientation()
params.setRotation()
but have not had any luck.
I only need the image to be in the correct orientation in
public void onPreviewFrame(byte[] data, Camera camera)
It does not matter if a saved image would be upside down or rotated. I am simply passing the data on to my Unity Project and the only purpose of the Android Plugin is to pass the camera data to Unity. As long as the image data is correct inside OnPreviewFrame I would be all set.
I know there is a newer Camera2 API and that it deprecates the original Camera, but I would really like to be able to fix this with my existing plugin without having to write a new plugin that uses Camera2.
Here is a link to my project.
https://drive.google.com/open?id=1MD-NRVf0YhhVIiRUOiBptvSwh9wtK3V7
Thanks in advance.
John Lawrie
Updates. Here is the source of my .java file for easier reference. If you look in AdjustOrientation you can see I have some code to try to adjust displayorientation. It appears to have no effect.
package com.test.camerapreview;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.support.annotation.Dimension;
import android.util.Log;
import android.util.Size;
import android.view.Display;
import android.view.Surface;
import com.unity3d.player.UnityPlayer;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class NativeCamera implements Camera.PreviewCallback {
public static NativeCamera instance;
public static String gameObjectTargetName;
private static Activity myActivity;
Camera mCamera;
SurfaceTexture texture;
int nativeTexturePointer = -1;
int prevHeight;
int prevWidth;
//
// Call this function first.
//
public static void Setup(String gameObjectName, Activity theActivity){
gameObjectTargetName = gameObjectName;
myActivity = theActivity;
instance = new NativeCamera();
}
public int startCamera(int idx, int width, int height) {
nativeTexturePointer = createExternalTexture();
texture = new SurfaceTexture(nativeTexturePointer);
mCamera = Camera.open(idx);
setupCamera(idx, width, height);
try {
mCamera.setPreviewTexture(texture);
mCamera.setPreviewCallback(this);
AdjustOrientation(width, height);
mCamera.startPreview();
Log.i("Unity", "JAVA: camera started");
} catch (IOException ioe) {
Log.w("Unity", "JAVA: CAM LAUNCH FAILED");
}
return nativeTexturePointer;
}
public void stopCamera() {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
Log.i("Unity", "JAVA: Camera stopped");
}
private int createExternalTexture() {
int[] textureIdContainer = new int[1];
GLES20.glGenTextures(1, textureIdContainer, 0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureIdContainer[0]);
return textureIdContainer[0];
}
#SuppressLint("NewApi")
private void setupCamera(int cameraID, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
params.setRecordingHint(true);
// params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
params.setPreviewFormat(17);
params.setZoom(0);
// 16 ~ NV16 ~ YCbCr
// 17 ~ NV21 ~ YCbCr ~ DEFAULT *
// 4 ~ RGB_565
// 256~ JPEG
// 20 ~ YUY2 ~ YcbCr ...
// 842094169 ~ YV12 ~ 4:2:0 YCrCb comprised of WXH Y plane, W/2xH/2 Cr & Cb. see documentation *
// params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
Camera.Size previewSize = getOptimalSize(width, height, mCamera.getParameters().getSupportedPreviewSizes());
int previewWidth = previewSize.width;
int previewHeight = previewSize.height;
params.setPictureSize(previewWidth, previewHeight);
params.setPreviewSize(previewWidth, previewHeight);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setExposureCompensation(0);
//
// Fix the orientation.
//
// int orientation = detectCameraDisplayOrientation(cameraID);
// mCamera.setDisplayOrientation(orientation);
// params.setRotation(orientation);
// Camera.CameraInfo info = new Camera.CameraInfo();
// Camera.getCameraInfo(cameraID, info);
// Log.d("Unity", "info.orientation = " + info.orientation);
// params.setRotation(-90);
// mCamera.setDisplayOrientation(-90);
// mCamera.setDisplayOrientation(90);
// params.setRotation(90);
try{
mCamera.setParameters(params);
} catch (Exception e){
Log.i("Unity", "ERROR: " + e.getMessage());
}
Camera.Size mCameraPreviewSize = params.getPreviewSize();
prevWidth = mCameraPreviewSize.width;
prevHeight = mCameraPreviewSize.height;
int[] fpsRange = new int[2];
params.getPreviewFpsRange(fpsRange);
String previewFacts = mCameraPreviewSize.width + "x" + mCameraPreviewSize.height;
if (fpsRange[0] == fpsRange[1]) {
previewFacts += " #" + (fpsRange[0] / 1000.0) + "fps";
} else {
previewFacts += " #[" + (fpsRange[0] / 1000.0) + " - " + (fpsRange[1] / 1000.0) + "] fps";
}
Log.i("Unity", "JAVA: previewFacts=" + previewFacts);
}
private void AdjustOrientation(int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
Display display = myActivity.getWindowManager().getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0) {
Camera.Size previewSize = getOptimalSize(height, width, mCamera.getParameters().getSupportedPreviewSizes());
prevWidth = previewSize.width;
prevHeight = previewSize.height;
mCamera.setDisplayOrientation(90);
}
else if(display.getRotation() == Surface.ROTATION_90) {
Camera.Size previewSize = getOptimalSize(width, height, mCamera.getParameters().getSupportedPreviewSizes());
prevWidth = previewSize.width;
prevHeight = previewSize.height;
}
else if(display.getRotation() == Surface.ROTATION_180) {
Camera.Size previewSize = getOptimalSize(height, width, mCamera.getParameters().getSupportedPreviewSizes());
prevWidth = previewSize.width;
prevHeight = previewSize.height;
}
else { //if(display.getRotation() == Surface.ROTATION_270) {
Camera.Size previewSize = getOptimalSize(width, height, mCamera.getParameters().getSupportedPreviewSizes());
prevWidth = previewSize.width;
prevHeight = previewSize.height;
mCamera.setDisplayOrientation(180);
}
parameters.setPreviewSize(prevWidth, prevHeight);
mCamera.setParameters(parameters);
}
private Camera.Size getOptimalSize(int width, int height, List<Camera.Size> sizes) {
if(mCamera == null)
return null;
final double ASPECT_TOLERANCE = 0.1;
double targetRatio=(double)width / height;
if (sizes == null)
return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetWidth = width;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
Log.i("Unity", "RES: size=" + size.width + "/" + size.height + " Aspect Ratio: " + ratio + " target width: " + width + " target height: " + height);
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
continue;
}
if (Math.abs(size.width - targetWidth) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.width - targetWidth);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.width - targetWidth) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.width - targetWidth);
}
}
}
Log.i("Unity", "optimal size=" + optimalSize.width + "/" + optimalSize.height + "/ Aspect Ratio: " + (double) optimalSize.width / optimalSize.height);
return optimalSize;
}
public int getPreviewSizeWidth() {
return prevWidth;
}
public int getPreviewSizeHeight() {
return prevHeight;
}
public String GetPreviewSizes(int id) {
Camera cam = Camera.open(id);
Camera.Parameters params = cam.getParameters();
Gson gson = new Gson();
String JSON = gson.toJson(params.getSupportedPreviewSizes());
cam.release();
Log.d("Unity", "Supported sizes are " + JSON);
return JSON;
}
public byte[] bytes;
#Override
public void onPreviewFrame(byte[] data, Camera camera) {
bytes = data;
UnityPlayer.UnitySendMessage(gameObjectTargetName, "GetBuffer", "");
}
}
Despite spending a lot of time trying to get the camera to rotate the image with SetDisplayOrientation, for some reason none of that had any effect. I ended up doing what Alex Cohen suggested and put manually rotated the image in OnPreviewFrame
This is a link to the page that gives the code to how to rotate the image.
Rotate an YUV byte array on Android
in camera preview... set `mCamera.setPreviewDisplay(holder);
mCamera = Camera.open(0);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
mCamera.setDisplayOrientation(90);`
Camera.setDisplayOrientation only affects the preview output that's passed to Camera.setPreviewDisplay or Camera.setPreviewTexture. That's documented in the API reference for setDisplayOrientation:
This does not affect the order of byte array passed in onPreviewFrame(byte[], Camera), JPEG pictures, or recorded videos.
If you need to adjust the output of onPreviewFrame, then you need to do it yourself. The rotation you need to apply is the same value you'd pass to setDisplayOrientation, in the clockwise direction.
I have a camera preview at the top of the screen with a specific height and width. I am using front camera to generate the preview. The camera is working but the issue is the preview shows the bottom part of what the camera sees.
I am holding the camera right in front of the face and it should show the entire face.
This is my surfaceview class:
public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private List<Camera.Size> mSupportedPreviewSizes;
private Camera.Size mPreviewSize;
public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
for(Camera.Size str: mSupportedPreviewSizes)
Log.e("Sizes", str.width + "/" + str.height);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight,
int height) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
Log.e("Surface changed", "surfaceChanged => w=" + weight + ", h=" + height);
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
Camera.Parameters parameters = mCamera.getParameters();
// parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(mHolder);
//mediaRecorder.setOrientationHint(90);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (Exception e) {
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
if (mPreviewSize != null) {
float ratio;
if (mPreviewSize.height >= mPreviewSize.width)
ratio = (float) mPreviewSize.height / (float) mPreviewSize.width;
else
ratio = (float) mPreviewSize.width / (float) mPreviewSize.height;
// One of these methods should be used, second method squishes preview slightly
setMeasuredDimension(width, (int) (width * ratio));
// setMeasuredDimension((int) (width * ratio), height);
}
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
if (sizes == null)
return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.height / size.width;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
}
And xml:
<FrameLayout
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</FrameLayout>
I tried manually putting values to parameters.setPreviewSize(300,200) but it just shows a black screen in the preview. I am new to camera API in android and unfortunately I am running short of time to read google docs to understand the principle of camera api and manipulate my code accordingly.
Anny help would be appreciated.
You can try to change the setMeasuredDimension params into onMeasure method.
You cannot hardcode 300×200 because you can only choose one of supported preview sizes.But in your case, the width may different for different devices, and the aspect ratio too.
Actually, choosing 300×200 is not good for another reason: even though your preview height is hardcoded to 200, it is 200dp, but the camera preview works with real pixels. The best result (no pixelization) is achieved when your preview size is not less than the (real pixel) size of the preview surface on the screen.
Your code for MyCameraSurfaceView class looks good. The question is how you insert it into the videoview frame. You should set the layout parameters to Gravity.CENTER. This will make your preview equally cropped from top and bottom. If you want to send all pixels to display, you can choose to either distort the image, or to keep margins (similar to how youtube shows narrow videos).
Normally the android camera api with a PreviewCallback freezes the camera after a picture is taken and this can be used as an approval screen for the image taken. My problem is this is not the behavior on phones like the samsung S6 and S6 edge. those two phones continue with the live preview after the picture is taken.
The pictures taken are still good and can be used for later use but the preview isnt showing, and the way my app is set up, i have a check and an x mark for the user to approve what they just took a photo of. Instead they have this overlay for check and "x" and a live preview on the background. And this only happens on the samsung s6/s6 edge.
Any idea what can cause an issue like this on specific devices?
Code used for preview and picture taking with some logging everywhere:
class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
// SurfaceHolder
private SurfaceHolder mHolder;
// Our Camera.
private Camera mCamera;
// Parent Context.
private Context mContext;
// Camera Sizing (For rotation, orientation changes)
private Camera.Size mPreviewSize;
// List of supported preview sizes
private List<Camera.Size> mSupportedPreviewSizes;
private List<Camera.Size> mSupportPictureSizes;
// Flash modes supported by this camera
private List<String> mSupportedFlashModes;
// View holding this camera.
private View mCameraView;
public CameraPreview(Context context, Camera camera, View cameraView) {
super(context);
Log.d(TAG, "CameraPreview: ");
// Capture the context
mCameraView = cameraView;
mContext = context;
setCamera(camera);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// mHolder.setKeepScreenOn(true);
// deprecated setting, but required on Android versions prior to 3.0
// mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
/**
* Begin the preview of the camera input.
*/
public void startCameraPreview()
{
Log.d(TAG, "startCameraPreview: ");
try{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* Extract supported preview and flash modes from the camera.
* #param camera
*/
private void setCamera(Camera camera)
{
// Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
mCamera = camera;
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
mSupportPictureSizes = mCamera.getParameters().getSupportedPictureSizes();
for(Camera.Size size : mSupportedPreviewSizes){
Log.d(TAG, "supportedPreviewSizes width: " + size.width + " x " + size.height);
}
for(Camera.Size size : mSupportPictureSizes){
Log.d(TAG, "mSupportPictureSizes width: " + size.width + " x " + size.height);
}
mSupportedFlashModes = mCamera.getParameters().getSupportedFlashModes();
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// Set the camera to Auto Flash mode.
if (mSupportedFlashModes != null && mSupportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)){
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
}
mCamera.setParameters(parameters);
requestLayout();
}
/**
* The Surface has been created, now tell the camera where to draw the preview.
* #param holder
*/
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "surfaceCreated: ");
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Dispose of the camera preview.
* #param holder
*/
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "surfaceDestroyed: ");
if (mCamera != null){
mCamera.stopPreview();
}
}
/**
* React to surface changed events
* #param holder
* #param format
* #param w
* #param h
*/
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
Log.d(TAG, "surfaceChanged: preview surface does not exist");
return;
}
Log.d(TAG, "surfaceChanged: start & stop preview");
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
#Override
public SurfaceHolder getHolder() {
return super.getHolder();
}
/**
* Calculate the measurements of the layout
* #param widthMeasureSpec
* #param heightMeasureSpec
*/
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null){
Log.d(TAG, "onMeasure: w x h: " + width + " x " + height);
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
/**
* Update the layout based on rotation and orientation changes.
* #param changed
* #param left
* #param top
* #param right
* #param bottom
*/
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
Log.d(TAG, "onLayout: ");
// Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
if (changed) {
final int width = right - left;
final int height = bottom - top;
int previewWidth = width;
int previewHeight = height;
if (mPreviewSize != null){
Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation())
{
case Surface.ROTATION_0:
previewWidth = mPreviewSize.height;
previewHeight = mPreviewSize.width;
mCamera.setDisplayOrientation(90);
Log.d(TAG, "onLayout: rotation0");
break;
case Surface.ROTATION_90:
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
Log.d(TAG, "onLayout: rotation90");
break;
case Surface.ROTATION_180:
previewWidth = mPreviewSize.height;
previewHeight = mPreviewSize.width;
Log.d(TAG, "onLayout: rotation180");
break;
case Surface.ROTATION_270:
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
mCamera.setDisplayOrientation(180);
Log.d(TAG, "onLayout: rotation270");
break;
}
}
final int scaledChildHeight = previewHeight * width / previewWidth;
mCameraView.layout(0, height - scaledChildHeight, width, height);
}
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
Log.d(TAG, "getOptimalPreviewSize: ");
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE){
continue;
}
if (Math.abs(size.height - targetHeight) < minDiff) {
Log.d(TAG, "getOptimalPreviewSize: found a match");
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
Log.d(TAG, "getOptimalPreviewSize: couldn't find match");
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
Log.d(TAG, "getOptimalPreviewSize: " + optimalSize.width + " x HEIGHT: " + optimalSize.height);
return optimalSize;
}
}
/**
* Picture Callback for handling a picture capture and saving it out to a file.
*/
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size mPictureTakenSize = parameters.getPictureSize();
System.out.println("picture taken size: " + mPictureTakenSize.width + " x " + mPictureTakenSize.height);
if(data!=null){
Log.d(TAG, "onPictureTaken: data not null, setting data to currentData");
// currentData = data;
setPictureTakenData(data);
cameraViewModel.setPreview(true);
}
else{
onProgress = false;
Log.d(TAG, "onPictureTaken: data is null");
}
}
};
I just ran into the same issue. It looks like you can fix it by manually calling camera.stopPreview() in the onPictureTaken method of the PictureCallback.
The camera will not stop preview automatically after picture taken, so you need call mCamera.stopPreview() manually:
if (mCamera != null) {
try {
mCamera.stopPreview();
pauseFlash();
} catch (Exception e) {
e.printStackTrace();
}
}
// if current camera flash mode is TORCH, you need pause it.
private void pauseFlash() {
if (currentFlashMode.equals(Camera.Parameters.FLASH_MODE_TORCH)) {
params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
}
}
And then, after do something and navigate back to camera preview:
#Override
public void startCameraPreview() {
if (mCamera != null) {
try {
mCamera.startPreview();
resumeFlash();
} catch (Exception e) {
e.printStackTrace();
// TAKE CARE IF YOUR CAMERA HAS BEEN RELEASED
}
}
}
void resumeFlash() {
flashChange(currentFlashMode);
}
And I have some performance tips: You should find the best picture size and preview size at the first time user use your app, store those in SharedPreferences and you can use after without re-caculate.
Hope this help.
I am using custom camera and working fine but the issue is image is saving with very low (poor) quality. To overcome with it , i have tried all suggestions and implementations. Like ,
parameters.setJpegQuality(100);
parameters.setPictureFormat(ImageFormat.JPEG);
this is not working. After that i have used
List<Size> sizes = cameraParams.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width > size.width)
size = sizes.get(i);
}
cameraParams.setPictureSize(mPictureSize.width, mPictureSize.height);
This is also not working. Its saving with poor quality still.
Note : Camera preview is showing proper with good quality but the issue is when saving captured image to sdcard folder.
Advanced help would be appreciated!!
Finally my issue solved.
Here I was setting parameters for camera preview before i was capturing the image
public void takePicture() {
mCamera.takePicture(new ShutterCallback() {
#Override
public void onShutter() {
}
}, new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
}
}, new PictureCallback() {
#Override
public void onPictureTaken(final byte[] data, Camera camera) {
data1 = data;
if (mCamera != null) {
mCamera.stopPreview();
}
}
});
}
So before i called this function in my fragment i have set parameters before this method.
mPreview.setParams(params);// This was the mistake what i have done !
mPreview.takePicture();
finally solved after removing mPreview.setParams(params);
I can show you methods for resetting preview size:
You should change your parameters of preview at Camera.
private void setImageSize() {
Camera.Size size = CameraUtil.findClosetImageSize(mRxCamera.getNativeCamera(), PREFER_SIZE);
mCamera.getNativeCamera().getParameters().setPictureSize(size.width, size.height);
WIDTH = size.width;
HEIGHT = size.height;
}
and after You need to change layout sizes
private void resetPreviewSize() {
final boolean widthIsMax = mWidth > mHeight;
final Camera.Size size = mCamera.getNativeCamera().getParameters().getPreviewSize();
final RectF rectDisplay = new RectF();
final RectF rectPreview = new RectF();
rectDisplay.set(0, 0, mWidth, mHeight);
final int width = widthIsMax ? size.width : size.height;
final int height = widthIsMax ? size.height : size.width;
rectPreview.set(0, 0, width, height);
final Matrix matrix = new Matrix();
matrix.setRectToRect(rectDisplay, rectPreview, Matrix.ScaleToFit.START);
matrix.invert(matrix);
matrix.mapRect(rectPreview);
mCameraView.getLayoutParams().height = (int) (rectPreview.bottom);
mCameraView.getLayoutParams().width = (int) (rectPreview.right);
mCameraView.requestLayout();
}
and if you need
public static Camera.Size findClosetImageSize(Camera camera, Point preferSize) {
int preferX = preferSize.x;
int preferY = preferSize.y;
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> allSupportSizes = parameters.getSupportedPictureSizes();
Log.d(TAG, "all support Image size: " + dumpPreviewSizeList(allSupportSizes));
int minDiff = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < allSupportSizes.size(); i++) {
Camera.Size size = allSupportSizes.get(i);
int x = size.width;
int y = size.height;
int diff = Math.abs(x - preferX) + Math.abs(y - preferY);
if (diff < minDiff) {
minDiff = diff;
index = i;
}
}
return allSupportSizes.get(index);
}
I am Working on one barcode scanner application.
my barcode scanner and surface view works perfectly. but, while maintaing aspect ratio of camera, parent view layout is cut slightly from bottom in higher version android devices like nexus and motog as shown in first image. in lower version android devices i didn't face this issue like micromax canvas.
Here are two images,
in 1st image, 1st tab containing Camera view, and parent view cut slightly from bottom during maintaing aspect ratio of camera and in 2nd image of 2nd tab it's look perfect.
Here is my cameraPreview class,
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private List<Camera.Size> mSupportedPreviewSizes;
private Camera.Size mPreviewSize;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;
#SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera,
PreviewCallback previewCb, AutoFocusCallback autoFocusCb) {
super(context);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
// supported preview sizes
mSupportedPreviewSizes = mCamera.getParameters()
.getSupportedPreviewSizes();
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// Log.d("DBG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Camera preview released in activity
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
/*
* If your preview can change or rotate, take care of those events here.
* Make sure to stop the preview before resizing or reformatting it.
*/
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or reformatting changes
// here
// start preview with new settings
try {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
parameters.setPictureSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
mCamera.setPreviewCallback(previewCallback);
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e) {
// Log.d("DBG", "Error starting camera preview: " + e.getMessage());
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(),
widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(),
heightMeasureSpec);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width,
height);
}
float ratio;
if (mPreviewSize.height >= mPreviewSize.width)
ratio = (float) mPreviewSize.height / (float) mPreviewSize.width;
else
ratio = (float) mPreviewSize.width / (float) mPreviewSize.height;
// One of these methods should be used, second method squishes preview
// slightly
setMeasuredDimension(width, (int) (width * ratio));
// setMeasuredDimension((int) (width * ratio), height);
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w,
int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
if (sizes == null)
return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.height / size.width;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
}
any help would be appreciated.
Finally, I found a little solution for this,
i still didn't get what is the really issue was, but i just set padding for my frame layout as below in which i am adding surface view, and it's resolve my issue.
android:padding="0.2dp"
and my framelayout as below in wihch i am adding surface view,
<FrameLayout
android:id="#+id/layout_frameScanner"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:background="#color/color_black"
android:padding="0.2dp" >
</FrameLayout>