11

I have built a custom Camera App and I am trying to change the resoloution of the image that is took. I have read around that this could depend on the phone or version of Android?

I know they are set using the setParameters but just dont know how to set the actuall resoloution to work on all phones. I am wanting it to be kind of small as my app force closes otherwise. When I use a test picture at 640x348 this works so around that size/resoloution would be perfect.

It may be easier to use setPictureSize?

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera = Camera.open();
    try {
           Camera.Parameters parameters = camera.getParameters();
           if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
              parameters.set("orientation", "portrait");
              camera.setDisplayOrientation(90);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(90);
           } else {
              parameters.set("orientation", "landscape");
              camera.setDisplayOrientation(0);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(0);
           }
          camera.setParameters(parameters);
          camera.setPreviewDisplay(holder);
      } catch (IOException exception) {
         camera.release();
       }
        camera.startPreview();
    }
Matt
  • 1,747
  • 8
  • 33
  • 58

2 Answers2

19

There is no setResolution(), only setPictureSize(). Use getSupportedPictureSizes() on Camera.Parameters to find the size you want, or use that information to populate a ListView or Spinner or something for the user to choose the desired size. Here is a sample project recently updated to use getSupportedPictureSizes() to find the smallest supported resolution and use that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok cool is it possible for you to show me how to implement that I tried running your code from github but just getting errors from eclipse? – Matt Jan 06 '12 at 10:48
  • Also will the code check the supported sizes and then save it as the smallest, or does it need more? – Matt Jan 06 '12 at 10:53
  • @Matt: I cannot help you with "errors from eclipse" if you refuse to say what the errors are. And I have no idea what "more" is, and so I cannot say if the code needs it. – CommonsWare Jan 06 '12 at 12:28
  • 1
    is it possible for you to join me in a Chat? – Matt Jan 06 '12 at 12:29
  • 2
    @Matt: If by "a Chat" you mean one of my office hours chats for subscribers to my books, sure. Beyond that, please open up a fresh StackOverflow question and ask your questions regarding this sample project. Tag it with `commonsware` and `android` so I pick it up, or post a link to the new question in a comment here. – CommonsWare Jan 06 '12 at 12:51
  • Question is here http://stackoverflow.com/questions/8757341/android-reduce-size-of-camera-picture – Matt Jan 06 '12 at 13:10
  • Remember that setPictureSize() is not supported for some devices, for example Samsung Galaxy NOTE 4 and others – PYK Apr 25 '18 at 04:46
14

It's too easy to capture image with high quality, here you can set your own resolution:

mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();

// Check what resolutions are supported by your camera
List<Size> sizes = params.getSupportedPictureSizes();

// Iterate through all available resolutions and choose one.
// The chosen resolution will be stored in mSize.
Size mSize;
for (Size size : sizes) {
    Log.i(TAG, "Available resolution: "+size.width+" "+size.height);
        mSize = size;
    }
}

Log.i(TAG, "Chosen resolution: "+mSize.width+" "+mSize.height);
params.setPictureSize(mSize.width, mSize.height);
mCamera.setParameters(params); 

Hope this will help you all.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • There is a list of resolutions and you can choose between a range. The highest resolution will be selected if you put a break; statement on the first find in this loop, and also you should avoid/check on runtime exception and OutOfMemory exception: if (size.width >= 600 && size.width <= 800) { mSize = size; break; } – Abhinav Saxena Nov 28 '16 at 05:02
  • @AbhinavSaxena, yes, even we can choose anyone of them as required. – Hiren Patel Nov 28 '16 at 05:53
  • That was not a query instead a solution. I have added my solution in the comments. You can modify in your answer. Thanks. – Abhinav Saxena Nov 28 '16 at 07:39