15

How to capture an square image in android? I want to capture an square image (such as 300x300 pixel) by calling Camera through intent in android, how can I do this?

Aleadam
  • 40,203
  • 9
  • 86
  • 108
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165

2 Answers2

9

EDIT: This is deprecated since API level 21.

Use the Camera.Size nested class

http://developer.android.com/reference/android/hardware/Camera.Size.html

From the android reference:

http://developer.android.com/reference/android/hardware/Camera.html

Class Overview

The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.

Make sure that the size is supported by the camera (and most probably it won't). If not, take a picture at the closest resolution and crop it or resize it.

Camera myCamera = Camera.open(0);
List<Camera.Size> sizes = myCamera.getPArameters().getSupportedPictureSizes();

To learn about camera intents, check these questions already in SO:

Android camera intent

Camera intent android

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • Thanks Aleadam, but I neet to capture a square image. There isn't any solution, isn't it? – Nguyen Minh Binh May 20 '11 at 16:30
  • @Nguyen : "*If not, take a picture at the closest resolution and crop it or resize it.*" I believe that's your only option (which would not be hard to implement anyway). – Aleadam May 20 '11 at 16:33
  • So, How can I can crop an image in android 2.x? I have no solution for this. Do you have any sample code? – Nguyen Minh Binh May 20 '11 at 16:36
  • Check the answers here: http://stackoverflow.com/questions/3846338/how-to-crop-an-image-in-android (specially the `com.android.camera.action.CROP` Intent) – Aleadam May 20 '11 at 16:47
  • the activity com.android.camera.action.CROP does not exists in android 2.x, Aleadam. – Nguyen Minh Binh May 20 '11 at 16:59
  • It does look like it's there: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.3_r1/com/android/camera/Camera.java#1192 . If not, check Shankar's or Dinesh's answers for alternative ways. – Aleadam May 20 '11 at 17:02
5

Why nobody mentioned something like this?

Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, xStart, yStart, 300, 300);

where sourceBitmap is original capture from camera and xStart and yStart location from where cropping will start. The result should have xStart, yStart in top left corner.

Ewoks
  • 12,285
  • 8
  • 58
  • 67
  • 4
    This would be a good option if the captured image is not too large for bitmap. I got an app that captures picture, but if I want to resize it this way, I get an outOfMemoryException, because the captured pic is too large. – Opiatefuchs Dec 16 '12 at 11:09
  • @Opiatefuchs That would suggest the device is not able of processing its own image captures. Which makes little sense. Have you tried ..? – Jaroslav Záruba Dec 09 '15 at 22:02
  • @JaroslavZáruba it doesn't mean that and it depends on several factors why outOfMemoryException happened on that specific device. Source bitmap should be scaled down first by some factor of two. – Ewoks Dec 11 '15 at 08:51
  • @JaroslavZáruba it doesn't mean that and it depends on several factors why outOfMemoryException happened on that specific device. Source bitmap should be scaled down first by some factor of two using BitmapFactory.Options, inScale parameter – Ewoks Dec 11 '15 at 08:58
  • @Ewoks If he has nothing else eating the camera then he should be able to load the bitmap in full size. – Jaroslav Záruba Dec 12 '15 at 10:04