Rotating images on android. Is there a better way?

后端 未结 3 1025
-上瘾入骨i
-上瘾入骨i 2020-12-31 13:48

I have an app that displays quite a few images for the user, and we\'ve been seeing a lot of error reports with OutOfMemoryError exception.

What we curr

3条回答
  •  感情败类
    2020-12-31 14:20

    2 methods of rotating a large image:

    1. using JNI , like on this post.

    2. using a file : it's a very slow way (depending on the input and the device , but still very slow) , which puts the decoded rotated image into the disk first , instead of putting it into the memory .

    code of using a file is below:

    private void rotateCw90Degrees()
      {
      Bitmap bitmap=BitmapFactory.decodeResource(getResources(),INPUT_IMAGE_RES_ID);
      // 12 => 7531
      // 34 => 8642
      // 56 =>
      // 78 =>
      final int height=bitmap.getHeight();
      final int width=bitmap.getWidth();
      try
        {
        final DataOutputStream outputStream=new DataOutputStream(new BufferedOutputStream(openFileOutput(ROTATED_IMAGE_FILENAME,Context.MODE_PRIVATE)));
        for(int x=0;x=0;--y)
            {
            final int pixel=bitmap.getPixel(x,y);
            outputStream.writeInt(pixel);
            }
        outputStream.flush();
        outputStream.close();
        bitmap.recycle();
        final int newWidth=height;
        final int newHeight=width;
        bitmap=Bitmap.createBitmap(newWidth,newHeight,bitmap.getConfig());
        final DataInputStream inputStream=new DataInputStream(new BufferedInputStream(openFileInput(ROTATED_IMAGE_FILENAME)));
        for(int y=0;y

提交回复
热议问题