3

Newbie user question:

I have searched and experimented for several days and need some help. I have probably read every SO posting on this topic but I still cannot figure it out.

I am trying to crop a specific area of a camera preview image. The issue is that the blue box is defined by a layout with top = 150 and left = 38, and height = 292 and width = 600. The image has height = 2448 and width = 3264.

Big box is the camera preview screen, small box is the portion I want to crop (apparently I don't yet have the ability to post pictures).

-----------------------------
|                           |
|                           |
|                           |
|   ---------               |
|   |       |               |
|   ---------               |
|                           |
-----------------------------

How do I crop the image so I am only left with the portion in the box? I'll be displaying it on another activity so I am not worried about that part.

Just to complicate things, the box size and image size changes depending on the phone, and I need to work with API 8 (version 2.2, Froyo).

I would also prefer not to save the image to a file because then I would have to encrypt it (the picture would be an image of a check for deposit).

I am using Camera.takePicture() and the jpegCallback as in the following code snippet (initialBitmap would be the "big box" above, and rl would be the "small box"):

    PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        if ( data != null && data.length > 0 ) {
            Bitmap initialBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            int bitmapHeight = initialBitmap.getHeight();
            int bitmapWidth = initialBitmap.getWidth();

            // get the "picture" frame
            RelativeLayout rl = (RelativeLayout) findViewById( R.id.clear_layout );

            int frameX = rl.getLeft();
            int frameY = rl.getTop();
            int frameHeight = rl.getHeight();

Hopefully there is a simple mathematical relationship between the view coordinates/dimensions and the bitmap.

I am also open to any better way to go about things but have tried to use the Preview data itself (too small) and manipulating the bitmap (out of memory errors).

Thanks in advance.

Gravitoid
  • 1,294
  • 1
  • 20
  • 20
  • If you know the position of the box you want to get, why are you getting information from a relative layout? Why not just use the information at the start of the question and Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) to get the subsection in a new bitmap? – Neil Townsend Mar 01 '13 at 18:55
  • Even though you said that have read everything image cropping, have you checked that answer? http://stackoverflow.com/a/9388244/1657165 – Rigotti Mar 01 '13 at 20:17
  • I understand how to crop an image. The issue I am having is I don't know the x, y, width, height in the image, based on the top, left, width, height of the RelativeLayout as shown on the activity layout. (i.e. the layout is 442 x 960 pixels but the jpeg picture is 2448 x 3264 pixels). What is the relationship? I've tried scaling but it seems that there is more picture outside of the preview that is causing the math to be off. – Gravitoid Mar 01 '13 at 20:52

1 Answers1

1

In case anyone cares, I never found an answer to this question. I ended up solving the problem a different way. I used the full preview area and just making my "take picture" button transparent enough to not be in the way. I also never cropped but the image size is close to what it would have cropped to.

From all that I learned over the last year I don't think there is a relationship between the preview size and the resulting jpg file size. The relationship seems to be different for different phone cameras.

If anyone has found a relationship I'd still be happy to learn about it.

Gravitoid
  • 1,294
  • 1
  • 20
  • 20