0

Currently i'm using a custom camera application, the preview looks just fines. But when i take the picture and save the devices the picture has decreased with like 80%(also size).Anyone knows why this is happening? Also in gallery the quality is poor. I am using this camera plugin

https://github.com/performanceactive/phonegap-custom-camera-plugin/tree/master/platforms/android/src/com/performanceactive/plugins/camera

and thats the paramaters. I can't add all codes(getPicture and Save codes). You can see from the link I shared.

CameraPrewiev...

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (getHolder().getSurface() == null) {
            return;
        }
        try {
            camera.stopPreview();
        } catch (Exception e){
            // tried to stop a non-existent preview
        }

        try {
            Camera.Parameters cameraParameters = camera.getParameters();
            Size previewSize = optimimalPreviewSize(width, height);
            cameraParameters.setPreviewSize(previewSize.width, previewSize.height);
            camera.setParameters(cameraParameters);
            camera.setPreviewDisplay(holder);
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
 private Size optimimalPreviewSize(int targetWidth, int targetHeight) {
        List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
        float targetAspectRatio = (float) targetWidth / targetHeight;
        List<Size> sizesWithMatchingAspectRatios = filterByAspectRatio(targetAspectRatio, sizes);
        if (sizesWithMatchingAspectRatios.size() > 0) {
            return optimalSizeForHeight(sizesWithMatchingAspectRatios, targetHeight);
        }
        return optimalSizeForHeight(sizes, targetHeight);
    }

    private List<Size> filterByAspectRatio(float targetAspectRatio, List<Size> sizes) {
        List<Size> filteredSizes = new ArrayList<Size>();
        for (Size size : sizes) {
            // reverse the ratio calculation as we've flipped the orientation
            float aspectRatio = (float)size.height / size.width;
            if (aspectRatio == targetAspectRatio) {
                filteredSizes.add(size);
            }
        }
        return filteredSizes;
    }

    private Size optimalSizeForHeight(List<Size> sizes, int targetHeight) {
        Size optimalSize = null;
        float minimumHeightDelta = Float.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minimumHeightDelta) {
                optimalSize = size;
                minimumHeightDelta = Math.abs(size.height - targetHeight);
            }
        }
        return optimalSize;
    }
Junior Develepor
  • 192
  • 2
  • 18

1 Answers1

2

When you set preview size, you should also set picture size. It is good practice to set both with same aspect ratio, otherwise some devices produce really bad stills. But you don't have to set them equal (e.g. preview of 800x480 works well with picture size 2560 x 1440). See also Camera in Android, how to get best size, preview size, picture size, view size, image distorted.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thank you for answer. The answer was very helpful . It will have one more question . How do I get the value of orientation where I try to save the image. But I need do without using eventListener . Because at that moment I just want the position . – Junior Develepor Jul 07 '15 at 08:16
  • *orientation* - you mean Portrait or Landscape? Is your activity defined locked to Landscape, or it follows the sensor? – Alex Cohn Jul 07 '15 at 11:30
  • Thank you , i mean Portrait. I locked to Portrait of UI . And i solved the problem. The process of rotation, I moved to the activity of the preview. – Junior Develepor Jul 07 '15 at 14:02