Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Camera Size - Parameters vs Intent?

I am currently using an intent to take a picture, like this:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_REQUEST);

But I really need to set the image size as close to a square as possible. So after researching it seems you need to do something like this:

Camera camera = Camera.open();
Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();

// Once you will see the supported Sizes, you can define them with the method :

setPictureSize(int width, int height);

My questions are, do these work together or is it an either/or? Which method works best for my needs?

Again, I have 96px by 96px box for user profile pics. After user takes the picture I want the image to fill entire box without stretching. Is the best way to set size at the point of picture being taken, or alter the imageView or bitmap (or ...?) after the fact? OR, should I just let the users crop the picture and I can define the cropping area dimensions?

Edit: See bounty for updated question.

like image 711
TheLettuceMaster Avatar asked Mar 15 '13 16:03

TheLettuceMaster


People also ask

What is camera intent?

Take a photo with a camera appAndroid delegates actions to other applications by invoking an Intent . This process involves three pieces: the Intent itself, a call to start the external Activity , and some code to handle the image data when focus returns to your activity.

How do I open camera with intent?

The opening of the Camera from inside our app is achieved with the help of the ACTION_IMAGE_CAPTURE Intent of MediaStore class. This image shows the Image clicked by the camera and set in Imageview. When the app is opened, it displays the “Camera” Button to open the camera.


3 Answers

Do these work together or is it an either/or? Which method works best for my needs?

No, they do not work together. When you use the camera with an Intent, you are asking the system to take a picture for you (i.e. the user gets prompted with the default camera app and takes a picture or chooses one from gallery).

When you use the second approach, you create your own Camera object, which is much more customizable than the first one.

With the first method you will have to scale the image afterwards, while with the second, you can take the picture directly in the correct size, so the main difference is that the first is controlled by android, and the second by your app directly, so the second method works best for your needs. if you don't want to scale afterwards.

If you're going to scale anyway (read below), then it doesn't matter which approach you use to take the picture, I'd say in that case, use the default app.

Is the best way to set size at the point of picture being taken, or alter the imageView or bitmap (or ...?) after the fact? OR, should I just let the users crop the picture and I can define the cropping area dimensions?

That depends on your needs. In the first case your app alters the picture taken, so you choose which portion of the original picture will be the final 96x96 picture. The problem with this method is that you can accidentally crop/strech/manipulate the image in a wrong -or at least, unexpected- manner for the user (i.e. removing part of their face).

When you use the second method, you are providint the user with the freedom to choose what part of the picture they want.

If you are able to automatically detect the best portion of the original image, you should go with the first method, but as you are actually asking which is the best method, you should really go with the second one, because it provides more flexibility to the end user.

like image 131
Twinone Avatar answered Sep 28 '22 08:09

Twinone


This code allows me to pick image from gallery. Crop and use my aspect ratio. I did similar code for using camera. After user takes picture, it immediately launches an activity to crop the picture.

  Intent photoPickerIntent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                photoPickerIntent.setType("image/*");
                photoPickerIntent.putExtra("crop", "true");
                photoPickerIntent.putExtra("outputX", 150);
                photoPickerIntent.putExtra("outputY", 150);
                photoPickerIntent.putExtra("aspectX", 1);
                photoPickerIntent.putExtra("aspectY", 1);
                photoPickerIntent.putExtra("scale", true);
                photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
                photoPickerIntent.putExtra("outputFormat",
                        Bitmap.CompressFormat.JPEG.toString());
                startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE);
like image 16
TheLettuceMaster Avatar answered Oct 22 '22 20:10

TheLettuceMaster


You can use Bitmap.createScaleBitmap(Bitmap src, int destWidth, int destHeight, boolean filter) to resize a bitmap

like image 6
Moesio Avatar answered Oct 22 '22 19:10

Moesio