Java 解码旋转的位图

Java 解码旋转的位图,java,android,android-layout,bitmap,Java,Android,Android Layout,Bitmap,我正在寻找一种从旋转文件(0,90180270度)解码位图的方法。我需要它,因为图像很大,如果我解码位图,然后使用 Bitmap rotatedBitmap=Bitmap.createBitmap(source, x, y, width, height, m, filter) 有时两个位图都在内存中,存在内存不足的风险 有什么想法吗?谢谢。也许这就是你要找的 //decodes image and scales it to reduce memory consumption /

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

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;
    }
//解码图像并对其进行缩放以减少内存消耗
//注意:如果图像的尺寸超过int-width和int-height
//它的尺寸将被改变。
专用位图解码LowResImage(字节[]b,整数宽度,整数高度){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的ByteArrayInputStream(b),null,o);
//我们要扩展到的新尺寸
所需的最终整型尺寸宽度=(整型)(宽度*0.7);
所需最终整型尺寸高度=(整型)(高度*0.7);
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2也许这就是你想要的

    //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;
    }
//解码图像并对其进行缩放以减少内存消耗
//注意:如果图像的尺寸超过int-width和int-height
//它的尺寸将被改变。
专用位图解码LowResImage(字节[]b,整数宽度,整数高度){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的ByteArrayInputStream(b),null,o);
//我们要扩展到的新尺寸
所需的最终整型尺寸宽度=(整型)(宽度*0.7);
所需最终整型尺寸高度=(整型)(高度*0.7);
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

if(width_tmp/2如果您希望使用基于NDK的解决方案,我已经创建了一个,并且创建了一个github项目

这将避免OOM,方法是将数据放入原生C“世界”,循环使用旧数据,并在旋转后返回结果


它不需要任何下采样。

如果您希望使用基于NDK的解决方案,我已经创建了一个,并且创建了一个github项目

这将避免OOM,方法是将数据放入原生C“世界”,循环使用旧数据,并在旋转后返回结果


它不需要任何下采样。

谢谢你的回答,我不想缩小它的大小,只要旋转它如果你想旋转一幅图像,你必须扩展ImageView类,覆盖它的onDraw方法,获取画布,在画布上应用旋转方法,最后在可绘制的表格上使用draw方法。谢谢你的回答,我不需要ant要减小其大小,只需旋转它如果要旋转图像,则必须扩展ImageView类,重写其onDraw方法,获取画布,在画布上应用rotate方法,最后在drawable上使用draw方法。