I am working on Android project, where the user needs to take picture of some card (credit card, insurance card, etc). The user needs to align their card in the rectangle shape, that I have drawn on the Phone screen when capturing the image. After image is taken, the App should crop out part of the picture which was aligned into the rectangle shape.


The problem occurs while I crop the image. The default Aspect Ratio (and this one can only be used) is 4:3 with the camera. However, the pictures taken have 16:4 Aspect Ratio. What does this mean is that the user sees (on his / her phone while trying to capture the image) limited field of what will actually be captured. After the capture, the image is larger (for e.g. if on a desk there is a card and few centimetres away is a pencil..the user does not see pencil on his phone screen, but after image is taken, he / she opens the image and sees it).


Please note that (interestingly for me) the picture is well aligned on the left, top and bottom side, but only extends further in right direction!


So I have the actual size of the picture, I know the dimensions of sides. But when I try to crop the image, I have to tell Android where to start the cropping (provide x,y coordinates of starting position to crop). From that point it will start to crop image in the right direction and bottom direction (this length I also have to provide, how far to right should it crop and how far to bottom).

I have had no luck to truly adjust this cropping area. I have to somehow calculate the correct dimensions of the crop rectangle area and the distance from the start of the picture and top of the picture (x,y coordinates). For example: If the crop area starts 40px from the left (on the phone screen).. this won't be the same if I apply 40px from the left on the actual (larger image). If I say, go down X pixels (where X is the length of rectangle side), it will take much less than it should (it will for instance select only half of the card, where it should take the entire card, since user aligned it within the rectangle).

Hope someone here can help me out with these calculations I have been struggling.

  • $\begingroup$ No, not directly, using library from Google. But not sure if this is the issue, I think issue is purely mathemtical, I just need to know where is the X,Y coordinate in the actual picture and how wide and long should the cropping area be from that X,Y coordinate. The dimensions are not the same as in Crop Area..probably because of the different aspect ratios. $\endgroup$ May 2, 2017 at 17:40
  • $\begingroup$ I'm having difficulty imaging how mathematical reasoning would help as much as trial and error to find the optimal cropping strategy. This is a puzzle, why you are having "no luck to truly adjust this cropping area". It may help you design instructions for users if you figure out the interaction between the user-centered cropping and the results you get. $\endgroup$
    – hardmath
    May 2, 2017 at 17:43
  • $\begingroup$ Just download one of the captured images to some image-processing software (GIMP comes to mind) that will tell you exactly how many pixels wide and how many pixels high it really is, and that will let you move a crosshairs to various points in the image and will tell you the coordinates of those points relative to the upper left hand corner. Then you can directly measure where the upper left corner of the cropped image should be, and how many pixels of width and height to ask for. $\endgroup$
    – David K
    May 2, 2017 at 17:52
  • $\begingroup$ David K we have very similar nicknames, I was wondering when did I post that comment :D But sounds like a good plan. Do you think this messurements will apply for all devices? Or only for my specific device with my specific camera ? $\endgroup$ May 2, 2017 at 18:23


You must log in to answer this question.

Browse other questions tagged .