Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
dx22
Posts: 402
Registered: ‎11-26-2010
My Device: Torch
Accepted Solution

How to rotate big images

I tried the code from this article:

http://supportforums.blackberry.com/t5/Java-Development/Rotate-and-scale-bitmaps/ta-p/492524

 

but it doesn't work for bigger images like 1024x768 and throws IllegalArgumentAxception saying that the bitmap is too big.

----------------------------------------------------------------------
Press the button to give kudos if I helped you :smileyhappy:
Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to rotate big images

I have found this as well, though I have never looked very hard for a fix because I figured it is unlikely that a BlackBerry with a screen size of 320 by 240 say, would have the capability of processing an image that was significantly bigger that it could ever likely display.  So I tiled the image, or more accurately, split the image up into a number of tiles. 

 

What RIM command was being executed when you got this exception thrown.  I think mine was when I tried to create a Bitmap to copy the image into. 

Please use plain text.
Developer
dx22
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: How to rotate big images

I had a line like this:
Graphics g = new Graphics(bitmap);
The bitmap itself is created successfully.
----------------------------------------------------------------------
Press the button to give kudos if I helped you :smileyhappy:
Please use plain text.
Developer
dx22
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: How to rotate big images

OK I did something which works for me but isn't perfect at all.

It works for 1024x768 images well and rotates them but increases the file size(if you save them) for some reason and also loses some of the brightness but the quality is ok at least for the purposes of my application.

 

Here is what I do:

1)I take a bitmap(1024x768) and create a new bitmap with 1024x1024 - equal width and height

2)copy the old one on the new one

3)rotate the new bitmap as you would rotate a NxN sized matrix and return a new rotated bitmap

4)create a final new bitmap from the rotated one but cut the added part so you get a 768x1024 sized image

 

Here is the code. It could be optimized further especially in the rotateSquareImage() method where you could work with regular arrays not 2d arrays.

 

package com.melonmobile.blackberry.pdfscanner.screens;

import net.rim.device.api.system.Bitmap;

public class ImageRotator {

	public Bitmap rotate(Bitmap oldImage) throws Exception {
		int w = oldImage.getWidth();
		int h = oldImage.getHeight();
		int d = Math.max(w, h);
		int amountPxAdded = 0;
		if (w > h) {
			amountPxAdded = w - h;
		}
		Bitmap oldBitmapSquared = new Bitmap(d, d);
		int[] data = new int[w * h];
		oldImage.getARGB(data, 0, w, 0, 0, w, h);
		oldBitmapSquared.setARGB(data, 0, w, 0, 0, w, h);
		Bitmap finalRes = new Bitmap(h, w);
		int fW = finalRes.getWidth();
		int fH = finalRes.getHeight();
		oldImage = null;
		Bitmap rotated = rotateSquareImage(oldBitmapSquared, 90);
		oldBitmapSquared = null;
		data = new int[fW * fH];
		rotated.getARGB(data, 0, fW, amountPxAdded, 0, fW, fH);
		rotated = null;
		finalRes.setARGB(data, 0, fW, 0, 0, fW, fH);
		return finalRes;
	}

	private Bitmap rotateSquareImage(Bitmap oldB, int angle) throws Exception {
		int w = oldB.getWidth();
		int h = oldB.getHeight();
		Bitmap newB = new Bitmap(w, h);
		int[] oldD = new int[w * h];
		int[] newD = new int[w * h];
		oldB.getARGB(oldD, 0, w, 0, 0, w, h);
		int[][] old2d = new int[h][w];
		int[] js;
		int old2dLen = old2d.length;
		int jsLen;
		for (int i = 0; i < old2dLen; i++) {
			js = old2d[i];
			jsLen = js.length;
			for (int j = 0; j < jsLen; j++) {
				js[j] = oldD[i * w + j];
			}
		}
		int[][] rotated = new int[h][w];
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				rotated[i][j] = old2d[h - j - 1][i];
			}
		}
		old2d = null;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				newD[i * w + j] = rotated[i][j];
			}
		}
		rotated = null;
		newB.setARGB(newD, 0, w, 0, 0, w, h);
		return newB;
	}
}

 

 

If someone decides to improve it please post the improved version so it could be used by more people.

 

----------------------------------------------------------------------
Press the button to give kudos if I helped you :smileyhappy:
Please use plain text.
Developer
dx22
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: How to rotate big images

Here is a better version of the code from the previous post. This is way faster.

 

package com.melonmobile.blackberry.pdfscanner.screens;

import net.rim.device.api.system.Bitmap;

public class ImageRotator {

	public Bitmap rotate(Bitmap oldImage) throws Exception {
		int w = oldImage.getWidth();
		int h = oldImage.getHeight();
		int d = Math.max(w, h);
		int amountPxAdded = 0;
		if (w > h) {
			amountPxAdded = w - h;
		}
		Bitmap oldBitmapSquared = new Bitmap(d, d);
		int[] data = new int[w * h];
		oldImage.getARGB(data, 0, w, 0, 0, w, h);
		oldBitmapSquared.setARGB(data, 0, w, 0, 0, w, h);
		Bitmap finalRes = new Bitmap(h, w);
		int fW = finalRes.getWidth();
		int fH = finalRes.getHeight();
		oldImage = null;
		Bitmap rotated = rotateSquareImage(oldBitmapSquared);
		oldBitmapSquared = null;
		data = new int[fW * fH];
		rotated.getARGB(data, 0, fW, amountPxAdded, 0, fW, fH);
		rotated = null;
		finalRes.setARGB(data, 0, fW, 0, 0, fW, fH);
		return finalRes;
	}

	private Bitmap rotateSquareImage(Bitmap oldB) throws Exception {
		int w = oldB.getWidth();
		int h = oldB.getHeight();
		int[] oldD = new int[w * h];
		oldB.getARGB(oldD, 0, w, 0, 0, w, h);
		oldB = null;
		int[] newD = new int[w * h];
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				newD[i * w + j] = oldD[(h - j - 1)*w + i];
			}
		}
		oldD = null;
		Bitmap newB = new Bitmap(w, h);
		newB.setARGB(newD, 0, w, 0, 0, w, h);
		newD = null;
		return newB;
	}
}

 

 

 

----------------------------------------------------------------------
Press the button to give kudos if I helped you :smileyhappy:
Please use plain text.