3

I've looked into figuring out how to set the size of a picture, and I stumbled upon CameraParameters and the setPictureSize method associated with it. The problem is, I can't figure out how to use any of these. I don't know what needs to be imported, what object to create, how to use the setPictureSize method, or where to even place that code. I have 2 chunks of code that I feel may be a good place to use setPictureSize. These chunks are:

    takePicture = (Button) findViewById(R.id.takePicture);

    takePicture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (name.getText().length() == 0 || experimentInput.getText().length() == 0) {
                showDialog(DIALOG_REJECT);
                return;
            }

            ContentValues values = new ContentValues();
            imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

            time = getTime() ;

            startActivityForResult(intent, CAMERA_PIC_REQUESTED);
        }

    });

and:

public static File convertImageUriToFile (Uri imageUri, Activity activity)  {
    if (activity == null) Log.d("test", "NULL!");
    Cursor cursor = null;
    try {

        String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
        cursor = activity.managedQuery( imageUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            @SuppressWarnings("unused")
                String orientation =  cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if(cursor!=null)
    {
        //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    else return null;
}

So, my question is, how do I use setPictureSize and where should I put it? No website, nor the android development guide, nor any other related StackOverflow question was helpful to me.

Editted Code:

public Bitmap getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            int desiredImageWidth = 100;  // pixels
            int desiredImageHeight = 100; // pixels
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL
            Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(cursor.getString(column_index), o), 
                                                       desiredImageWidth, 
                                                       desiredImageHeight, 
                                                       false);

            return newImage; //cursor.getString(column_index);
        }
        else return null;
    }
Mxyk
  • 10,678
  • 16
  • 57
  • 76

1 Answers1

2

That's used for when you're actually using the camera through a custom View. What you're doing right here is sending an implicit intent to Android to open another Activity that uses the camera to take a picture.

In this case, you can limit the size of your image by using MediaStore.EXTRA_SIZE_LIMIT. If you want to specify the dimensions of the image, than you have to load it from the URI that it was saved to, create a new scaled image, then resave it over the old one.

Once you have the URI of the image (which is from the DATA attribute in the projection), you can resize the image like so:

int desiredImageWidth = 100;  // pixels
int desiredImageHeight = 100; // pixels
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL
Bitmap newImage = Bitmap.createScaleBitmap(BitmapFactory.decodeFile(imagePath, o), 
                                           desiredImageWidth, 
                                           desiredImageHeight, 
                                           false);

Then save it over the old one.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • That sounds reasonable. Where exactly am I putting this though? Somewhere in the `convertImageUriToFile` method or the `getPath` method? – Mxyk Oct 11 '11 at 18:03
  • Personally I wouldn't put the bitmap encoding inside any one of those methods since each server their own purpose quite well. `imagePath` must be a String object directly to the image file (not an android content uri), and it looks like `getPath()` will retrieve exactly that for you. You should also be able to get that information from the `File` object your `convertImageUriToFile()` method returns using `getAbsolutePath()` or `getPath()`. – DeeV Oct 11 '11 at 18:32
  • I sort of understand what you're saying. To do what you mentioned, I must use a String object rather than an android content uri. My question is, being a novice Android programmer, what do I need to change to do this? Do I need to put `String[] projection` somewhere/return it to something? – Mxyk Oct 12 '11 at 16:21
  • Your `getPath()` method will return the string you need if it's successful. You can change it to a `getBitmap` type method if you want and use `cursor.getString(column_index);` as the imagePath. – DeeV Oct 12 '11 at 17:49
  • I added an edit to the bottom of my question. Is this what you mean? – Mxyk Oct 12 '11 at 18:13
  • I ran my app using the code I have above. The picture has not re-sized to 100x100. – Mxyk Oct 12 '11 at 18:18
  • Are you displaying it in a layout? If so, are the layout parameters match_parent, fill_parent or wrap_content? It will stretch the image out if the View is in match_parent or fill_parent. – DeeV Oct 12 '11 at 18:33
  • I'm sending the picture off to a website. It is still huge on the website, not limited to the size I want. – Mxyk Oct 13 '11 at 13:48
  • Do you think maybe perhaps this is a problem not so much on my phone, but on the website itself? It seems that the image on the website always fills the screen... – Mxyk Oct 13 '11 at 16:09
  • It could be. I don't know. I just know that the image scales on my phone when I do it and show it on a View. What you can do in this case is get the image from your phone. You can save it to an external SD card, then use the DDMS in Eclipse to access the file system. Download the image from the phone and see what dimensions it is. – DeeV Oct 13 '11 at 17:09
  • The dimensions are coming out as 1536 x 2048. They should be 1024 x 786 (which is actually half of that). – Mxyk Oct 13 '11 at 17:39
  • Would you know how to convert from a Bitmap to a File? I see in my code originally its trying to covert a Uri to a File using a method, so I instead made this a Bitmap to a File (the Bitmap being the one you had me implement in the code you provided), perhaps this would work. I have a feeling its just ignoring the Bitmap code at this moment. – Mxyk Oct 13 '11 at 17:43
  • http://stackoverflow.com/questions/649154/android-bitmap-save-to-location I think the answer to that question would be what you're looking for. `fileName` in this context would be the direct path of the file you want to write too (can be a String of the URL or a File object). Compress `newImage` to the `outputstream` which should write it to the file. The solution at the top may work too. – DeeV Oct 13 '11 at 17:55
  • Converting an already captured photo to a different resolution will distort the image if `actualPhotoHeight / intendedPhotoHeight != actualPhotoWidth / intendedPhotoWidth`. Simply put, image will be distorted if scale ratio of sides doesn't match – Farid Mar 19 '21 at 07:44