创建缩放位图会导致图像无效

发布于 2024-11-06 06:56:32 字数 1456 浏览 1 评论 0原文

我创建了一个函数,可以将位图直接缩放到特定的表面积。该函数首先获取位图的宽度和高度,然后找到最接近所需尺寸的样本尺寸。最后将图像缩放至精确尺寸。这是我能找到的解码缩放位图的唯一方法。问题是从 BitmapFactory.createScaledBitmap(src,width,height,filter) 返回的位图总是以 -1 的宽度和高度返回。我已经实现了使用 createScaledBitmap() 方法的其他函数,但没有出现此错误,并且我找不到创建缩放位图会产生无效输出的任何原因。我还发现,如果我创建可变的图像位图副本,则会导致相同的错误。谢谢,

public static Bitmap load_scaled_image( String file_name, int area) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file_name, options);
    double ratio = (float)options.outWidth / (float)options.outHeight;
    int width, height;
    if( options.outWidth > options.outHeight ) {
        width = (int)Math.sqrt(area/ratio);
        height = (int)(width*ratio);
    }
    else {
        height = (int)Math.sqrt(area/ratio);
        width = (int)(height*ratio);
    }
    BitmapFactory.Options new_options = new BitmapFactory.Options();
    new_options.inSampleSize = Math.max( (options.outWidth/width), (options.outHeight/height) );
    Bitmap image = BitmapFactory.decodeFile(file_name, new_options);
    return Bitmap.createScaledBitmap(image, width, height, true);
}

我添加了这个功能来将大型相机图像缩放到特定数量的百万像素。因此,1 兆像素传入的典型区域为 1000000。解码后的相机图像产生的 outWidth 为 1952,outHeight 为 3264。然后我通过这种方式计算比率,我可以与缩放图像保持相同的高宽比,在本例中,比率为 0.598...使用比率和新的表面积我可以找到新的宽度为 773,高度为 1293。 773x1293=999489 大约是1 兆像素。接下来,我计算解码新图像的样本大小,在本例中样本大小为 4,图像解码为 976x1632。所以我传递的宽度为 773,高度为 1293。

I've created a function that scales a bitmap directly to a specific surface area. The function first gets the width and height of the bitmap and then finds the sample size closest to the required size. Lastly the image is scaled to the exact size. This is the only way I could find to decode a scaled bitmap. The problem is that the bitmap returned from BitmapFactory.createScaledBitmap(src,width,height,filter) always comes back with a width and height of -1. I've already implemented other functions that use the createScaledBitmap() method with out this error and I can not find any reason why creating a scaled bitmap would produce invalid output. I've also found that if I create a copy of the image bitmap that is mutable causes the same error. Thanks

public static Bitmap load_scaled_image( String file_name, int area) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file_name, options);
    double ratio = (float)options.outWidth / (float)options.outHeight;
    int width, height;
    if( options.outWidth > options.outHeight ) {
        width = (int)Math.sqrt(area/ratio);
        height = (int)(width*ratio);
    }
    else {
        height = (int)Math.sqrt(area/ratio);
        width = (int)(height*ratio);
    }
    BitmapFactory.Options new_options = new BitmapFactory.Options();
    new_options.inSampleSize = Math.max( (options.outWidth/width), (options.outHeight/height) );
    Bitmap image = BitmapFactory.decodeFile(file_name, new_options);
    return Bitmap.createScaledBitmap(image, width, height, true);
}

I added this function to scale large camera images to a specific number of mega pixels. So a typical area passed in would be 1000000 for 1 megapixel. The camera image after being decoded yields a outWidth of 1952 and a outHieght of 3264. I then calculate the ratio this way I can keep the same height to width ratio with the scaled image, in this case the ratio is 0.598... Using the ratio and the new surface area I can find the new width which is 773 and a height of 1293. 773x1293=999489 which is just about 1 megapixel. Next I calculate the sample size for which to decode the new image, in this case the sample size is 4 and the image is decoded to 976x1632. So I'm passing in a width of 773 a height of 1293.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2024-11-13 06:56:32

我遇到了类似的问题(缩放位图的高度和宽度为 -1)。
遵循这个 stackOverflow 线程:
Android 如何创建运行时缩略图
我在调用该函数时尝试两次使用相同的位图:

imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, 
                                        THUMBNAIL_SIZE, false);

出于某种原因,这解决了我的问题,也许它也能解决你的问题。

I was having a similar problem (getting -1 for height and width of the scaled bitmap).
Following this stackOverflow thread:
Android how to create runtime thumbnail
I've tried to use the same bitmap twice while calling the function:

imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, 
                                        THUMBNAIL_SIZE, false);

For some reason, this solved my problem, perhaps it would solve yours too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文