Taking Photos “Simply”

The most important part of our app is the camera, and both iOS and Android are making it easy for us developers to use the hardware via their APIs. Our implementation on iOS went smoothly, as it always does on iOS, but on Android it is a whole other story.

To start the camera application on Android is really simple, you just make an intent,

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

and you start it,

startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)

the easiest thing in the world, two simple lines of code, and the camera is launched, easy peasy japanesey. The only problem is that you get the “boring” native camera application. So of course, you’ll have to make a custom one, and that is not as hard either. You request a camera instance, and tell it where to draw the preview, simple. But what if you want a square preview, like Tise, width and height equal to the width of the screen, 1:1 ratio? Then it is a whole other ballgame. Unlike iOS, that you find on a limited number of phones, namely the iPhones, Android appears on hundreds, maybe thousands (?) of different phones. All of the different phones have their own hardware, thus own camera, and the cameras has their own predefined preview resolutions. This sounds good, all I have to do is to find and pick the 1:1 resolution… but wait, there is no 1:1 preview resolutions. FUDGE!!!


Here are the available preview resolution on my Samsung Galaxy S4:

  • 1920 1080 (16:9)
  • 1440 1080 (4:3)
  • 1280 720 (16:9)
  • 1056 864 (11:9)
  • 960 720 (4:3)
  • 720 480 (3:2)
  • 640 480 (4:3)
  • 320 240 (4:3)
  • 176 144 (11:9)

As we see, no 1:1 ratio…

There are several solutions to this. We actually started out by adding a black view to the top and the bottom of the preview, hiding the two parts, making the preview a perfect square.

As of now we are looking at a very cool API called CWAC Camera, made by commonsguy (Mark Murphy), founder of CommonsWare, an eager open source programmer and stackoverflow contributor (over 14.000 answers). The CWAC Camera hides a ton of code and makes it easy to completely fill our 1:1 ratio preview view. This is done by cropping off some of the actual preview frame, also known as a “full-bleed preview”. This is actually pretty much the same as out first solution, but CWAC Camera does this out of the box, and simplifies our code, great success!