Gravitoid
Gravitoid

Reputation: 1402

Android - How crop jpeg using view layout dimensions

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.

Upvotes: 3

Views: 572

Answers (1)

Gravitoid
Gravitoid

Reputation: 1402

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.

Upvotes: 1

Related Questions