【问题标题】:Android: Cropping a bitmap (in a strange way)Android:裁剪位图(以一种奇怪的方式)
【发布时间】:2011-07-05 13:55:42
【问题描述】:

我正在制作一个要求用户为自己拍照的应用。

所有问题都源于图像必须为 480 x 800 的事实。 我正在使用的设备(摩托罗拉 Xoom)上的摄像头显然不支持该纵横比。

我正在寻找一种方法来拍摄更大尺寸的照片(1280 x 960 有足够大的高度),然后开始从两侧、顶部和底部刮掉大块。

因此,如果有人能提供一种方法来做到这一点(或更好的想法),我将不胜感激。

谢谢。

【问题讨论】:

    标签: android eclipse bitmap crop


    【解决方案1】:

    更好的想法是将图像的大小调整为最大 480 的宽度或最大 800 的高度,并使用适当的填充显示它。 这是一个简单的方法:

    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;
        }
    }
    

    【讨论】:

    • 干杯芽。我会试一试,让你知道结果!
    • 抱歉,有几个问题。我认为“destSizeX”和“destSizeY”参数是我应该输入的最大尺寸?
    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多