How to get a full size image from a custom front camera at full screen length

I am making an android application that uses the front camera of the device and it has to take a picture in portrait form and it would like to save the image in the same result as in the preview, but when I take a photo and save the bitmap the bitmap the image seems very awkward as it increases in size in length. It seems that I am doing something wrong that is compressing the camera image on save.

I searched a lot of links and found out that for this we need to set the aspect ratio. I still didn't get it. I need help with my coding because I am about to complete my application and it works best on samsung grand and S4, but it does not work well on Lg g3 and.

This is what I do with the surfaceview class. I only share the surface class. Since I know something is wrong here, the rest of the classes are easy. CameraPreview.java

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private Camera camera;
public static  SurfaceHolder holder;
private Activity activity;


public CameraPreview(Context context, AttributeSet attrs, Activity activity) {
    super(context, attrs);

}

public CameraPreview(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CameraPreview(Context context) {
    super(context);
}

public void init(Camera camera,Activity activity) {
    this.camera = camera;
    this.activity = activity;
    initSurfaceHolder();
}

@SuppressWarnings("deprecation") // needed for < 3.0
private void initSurfaceHolder() {
    holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    initCamera(holder);
}

private void initCamera(SurfaceHolder holder) {
    try {
        Camera.Parameters parameters = camera.getParameters();
        List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
        Camera.Size previewSize = previewSizes.get(0); //480h x 720w
        for(int i = 0 ; i<=6;i++){


            Log.v("preview sizes"+previewSizes.get(i));
            Camera.Size previewSizedemo = previewSizes.get(i);
            Log.v("preview size are" +previewSizedemo.width+""+ previewSizedemo.height+"of"+i);
            Camera.Size previewSizedemo1 = previewSizes.get(1);
            Log.v("preview size of 0 is" +previewSizedemo1.width+""+ previewSizedemo1.height);
        }

        parameters.setPreviewSize(previewSize.width,previewSize.height);

     parameters.setZoom(Camera.Parameters.FOCUS_DISTANCE_OPTIMAL_INDEX);

        camera.setParameters(parameters);
        requestLayout();
        camera.setPreviewDisplay(holder);
        camera.startPreview();



    } catch (Exception e) {
        Log.d("Error setting camera preview", e);
    }
}

@Override
public void surfaceChanged(SurfaceHolder mHolder, int format, int width, int height) {

    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

    Camera.Size previewSize = previewSizes.get(0); //480h x 720w

    parameters.setPreviewSize(previewSize.width,previewSize.height);


       requestLayout();

    camera.setParameters(parameters);

    Display display = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if(display.getRotation() == Surface.ROTATION_0) {
        camera.setDisplayOrientation(90);
        parameters.setPreviewSize(previewSize.width, previewSize.height);
    } else if(display.getRotation() == Surface.ROTATION_270) {
        camera.setDisplayOrientation(180);
        parameters.setPreviewSize(previewSize.width, previewSize.height);
    }

    camera.startPreview();





}// end surfaceChanged





@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e("TABACT"+"surfaceDestroyed()");
    camera.stopPreview();
    camera.setPreviewCallback(null);
    camera.release();
    camera = null;

    this.getHolder().removeCallback(this);


}

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.width / size.height;
        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;
}

      

Remember that I am only using the front camera. Please help me on this how can I take a full size image from the front camera without flipping to any other angle and then onto the portrait.

0


source to share





All Articles