0

I made my preview activity class.

Resolution choice i made good. While i am not taking photo everything looks good.
After calling mCamera.takePicture() i am getting my photo which have current resolution too.

But just after calling mCamera.takePicture() preview freeze and appears for some time my photo but it has wrong sizes.

Can i make this picture right?

Just found this question https://stackoverflow.com/questions/24134964/issue-with-camera-picture-taken-snapshot-using-surfaceview-in-android. It is just what i whant to solve.

Community
  • 1
  • 1

3 Answers3

1

First, get screen size of the device:

     public void getScreenSize() {

        Display display = getWindowManager().getDefaultDisplay();
        Point sizepoint = new Point();
        display.getSize(sizepoint);
        width = sizepoint.x;
        height = sizepoint.y;
    }

Then use these instructions to list the preview sizes supported by the device::

   Parameters p = Camera.getParameters();
   List<Size> size = p.getSupportedPreviewSizes();
   Size size_to_use  = getOptimalSize(size, width, height);

and finally set the preview sizes to the camera preview:

            p.setPreviewSize(size_to_use.width, size_to_use.height);
            myCamera.setParameters(p); 

Below, the getOptimalSize method (got from the net).

    private Size getOptimalSize(List<Size> sizes, int w, int h) {


        final double ASPECT_TOLERANCE = 0.2;        
        double targetRatio = (double) w / h;         
        if (sizes == null)             
            return null;          
        Size optimalSize = null;         
        double minDiff = Double.MAX_VALUE;          
        int targetHeight = h;          

        for (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);             
            }         
        }          
        // Cannot find the one match the aspect ratio, ignore the requirement     

        if (optimalSize == null)
        {
            minDiff = Double.MAX_VALUE;             
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff)
                {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight); 
                }
            }
        }


        return optimalSize;     
    }
andreasperelli
  • 1,034
  • 2
  • 11
  • 40
  • I use exactly this way. It helps to take right resolution for preview and for result photo. But when preview stops, while picture is taking, i see distort photo while preview starts again. But in result i get normal image. – user_android_dev Jul 31 '14 at 11:21
  • Did u declare 'Parameters p' static ? – andreasperelli Jul 31 '14 at 12:29
0

This might help you! Use this code for picture issue...

Size size = mSupportedPreviewSizes.get(previewSizeCounter);
parameters.setPictureSize(size.width, size.height);
Developer
  • 173
  • 6
0

If you have developed your own Camera API there are two things to take into consideration getSupportedPreviewSizes() and getSupportedPictureSizes() within camera parameters then choose the ones that are supported. it is up to you to choose and make necessary calculations to choose the right one.

Yourange
  • 158
  • 10