15

When I use following code, it ends up with outofmemory exception. After doing researh Render script looks like a good candidate. Where can I find sample code for similar operation and how can integrate it to my project.

public Bitmap rotateBitmap(Bitmap image, int angle) {
    if (image != null) {

        Matrix matrix = new Matrix();
        matrix.postRotate(angle, (image.getWidth()) / 2,
                (image.getHeight()) / 2);

        return Bitmap.createBitmap(image, 0, 0, image.getWidth(),
                image.getHeight(), matrix, true);
    }
    return null;
}
pats
  • 1,273
  • 2
  • 20
  • 43
  • Does it always return outofmem? What size is your bitmap? – Miloslaw Smyk Dec 10 '15 at 04:04
  • Not always, but occasionally. I think its because of the copy been created. I use Picasso library to download the image and resize it to fit half screen size – pats Dec 10 '15 at 04:26
  • @MiloslawSmyk my image is down sampled, image size is 110 KB, so its quite small. Anyway to do this using ScriptInstric operations of renderscript? – pats Dec 14 '15 at 12:09
  • @pats Are you looking for 90 degree rotation - http://stackoverflow.com/questions/12044674/android-rotate-image-without-loading-it-to-memory or arbitrary angle rotation (e.g. 27 deg clockwise) – Morrison Chang Dec 16 '15 at 06:49
  • Yes i already tried it out . But its realy difiuclt to setup scriptC stuff. Is there a way to rotate in multiples of 90 without setting up scriptc. I mean using scriptintric and only java for example – pats Dec 16 '15 at 11:32

2 Answers2

1

Basically rotating bitmap is a task of rotating 2D array without using additional memory. And this is the correct implementation with RenderScript: Android: rotate image without loading it to memory .

But this is not necessary if all you want is just to display rotated Bitmap. You can simply extend ImageView and rotate the Canvas while drawing on it:

canvas.save();
canvas.rotate(angle, X + (imageW / 2), Y + (imageH / 2));
canvas.drawBitmap(imageBmp, X, Y, null);
canvas.restore();

What about ScriptIntrinsic, since it's just a built-in RenderScript kernels for common operations you cannot do nothing above the already implemented functions: ScriptIntrinsic3DLUT, ScriptIntrinsicBLAS, ScriptIntrinsicBlend, ScriptIntrinsicBlur, ScriptIntrinsicColorMatrix, ScriptIntrinsicConvolve3x3, ScriptIntrinsicConvolve5x5, ScriptIntrinsicHistogram, ScriptIntrinsicLUT, ScriptIntrinsicResize, ScriptIntrinsicYuvToRGB. They do not include functionality to rotate bitmap at the moment so you should create your own ScriptC script.

Community
  • 1
  • 1
amukhachov
  • 5,822
  • 1
  • 41
  • 60
  • Thanks, I already tried the rederscript algo proposed in that link. Its really hard to set up scriptC stuff, given that confusion in versions in build tools. I was wondering is there a way to rotate using scriptinstric and javacode avoidng scriptC – pats Dec 19 '15 at 03:41
0

Try this code..

private Bitmap RotateImage(Bitmap _bitmap, int angle) {

    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    _bitmap = Bitmap.createBitmap(_bitmap, 0, 0, _bitmap.getWidth(), _bitmap.getHeight(), matrix, true);
    return _bitmap;
}

Use this code when select image from gallery.

like this..

     File _file = new File(file_name);
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inSampleSize = 1;
     Bitmap bitmap = BitmapFactory.decodeFile(file_name, options);

     try {
            ExifInterface exif = new ExifInterface(file_name);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                bitmap = RotateImage(bitmap, 90);
            } else if (orientation ==ExifInterface.ORIENTATION_ROTATE_270) {
                bitmap = RotateImage(bitmap, 270);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
      image_holder.setImageBitmap(bitmap);
Vimal Gajera
  • 497
  • 3
  • 9
  • Thanks for the response, but I dont see much difference, given that I assign the new image to the input image after new image is returned from the method – pats Dec 10 '15 at 04:53
  • for check difference , First you capture image from front camera, and then check it using this function and without using this function. – Vimal Gajera Dec 10 '15 at 05:02
  • Ohh sorry, I download the image from internet. – pats Dec 10 '15 at 05:04
  • @pats Ok , When you capture image from Front camera ,at a time rotated image save,thats why this function use. – Vimal Gajera Dec 10 '15 at 05:21