java - 解码旋转的位图

标签 java android android-layout bitmap

我正在寻找一种从文件旋转(0,90,180,270 度)解码位图的方法。我需要它,因为图像很大,如果我解码位图然后使用类似

Bitmap rotatedBitmap=Bitmap.createBitmap(source, x, y, width, height, m, filter)

有时两个位图都在内存中,存在内存不足的风险。

有什么想法吗?谢谢。

最佳答案

也许这就是您要找的东西?

    //decodes image and scales it to reduce memory consumption
    //NOTE: if the image has dimensions which exceed int width and int height
    //its dimensions will be altered.
    private Bitmap decodeToLowResImage(byte [] b, int width, int height) {
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o);

            //The new size we want to scale to
            final int REQUIRED_SIZE_WIDTH=(int)(width*0.7);
            final int REQUIRED_SIZE_HEIGHT=(int)(height*0.7);

            //Find the correct scale value. It should be the power of 2.
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE_WIDTH || height_tmp/2<REQUIRED_SIZE_HEIGHT)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o2);
        } catch (OutOfMemoryError e) {
            //handle this
        }
        return null;
    }

关于java - 解码旋转的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9668430/

相关文章:

android - 使用任务从 Firestore 中多次读取文档引用

Android:了解 DPI 并通过代码获取它?

java - 由于两个服务器实例,同一个 quartz 作业运行两次

android - onCellInfoChanged() 回调始终为空

java - 为什么输入错误值后输出是三行消息而不是一行?

android - 程序类型已存在 : BuildConfig

Android - 在 translationY() 动画上更新 View 的高度

java - 如何更改显示的布局

java - 为什么 https ://username:password@example. com 不起作用?

java - 如何设置一个Frame来在swing中显示一个新的Panel?