开发者

Android: Cropping a bitmap (in a strange way)

I'm making an app that requires the user to take a picture of themselves.开发者_StackOverflow中文版

The problems are all stemming from the fact that the image has to be 480 x 800. The camera on the device I'm using (the Motorola Xoom) obviously does not support that aspect ratio.

I'm looking for a way to take the picture in a bigger size (1280 x 960 has a large enough height) and then start shaving off large chunks from both sides, the top and the bottom.

So if any one can provide a way to do that (or a better idea) I would greatly appreciate it.

Thank you.


Better idea is to resize the image to max 480 in width or max 800 in height and the show it with with appropriate padding. Here is simple way to do that:

import android.graphics.Bitmap;

public class ImageUtils {
    public static Bitmap  BitmapResizeToMaxXorY(Bitmap srcBitmap, int destSizeX, int destSizeY) {
        Bitmap ret = null;

        if (srcBitmap != null && destSizeX > 0 && destSizeY > 0) {
            Float origSizeX  = new Float(srcBitmap.getWidth());
            Float origSizeY = new Float(srcBitmap.getHeight());

            if (origSizeX > destSizeX || origSizeY > destSizeY) {
                float origRatio =  origSizeX / origSizeY;
                float destRatio = destSizeX / destSizeY;

                float targetSizeX;
                float targetSizeY;

                if (origRatio >= destRatio) {
                    if (destSizeX >= origSizeX) {
                        targetSizeX = origSizeX;
                    } else {
                        targetSizeX = destSizeX;
                    }

                    targetSizeY = targetSizeX / origRatio;
                } else {
                    if (destSizeY >= origSizeY) {
                        targetSizeY = origSizeY;
                    } else {
                        targetSizeY = destSizeY;
                    }

                    targetSizeX = targetSizeY * origRatio;
                }

                ret = Bitmap.createScaledBitmap(srcBitmap, (int) targetSizeX, (int) targetSizeY, false);
            } else {
                // original image is smaller than destination sizes so we are returning original
                ret = srcBitmap;
            }           
        }

        return ret;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜