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
New Developer
Posts: 2
Registered: ‎03-17-2009
My Device: Not Specified

How to rotate an image?

We develop software on the Blackberry Storm and want to use animations. How can I rotate an image by single degree increments?

 

Thanks for your help.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to rotate an image?

Have you done a search to see if anyone else has asked this question.  One I found was:

 

how to rotate Image or Graphic

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=14748

New Developer
Posts: 2
Registered: ‎03-17-2009
My Device: Not Specified

Re: How to rotate an image?

Thanks Peter. I saw that thread. But I am wondering if there are any RIM API doing this job.
New Developer
Posts: 23
Registered: ‎12-29-2008
My Device: Not Specified

Re: How to rotate an image?

what came out of that thread might help you.  you could get the J2ME army knife, go through the hassle building a .cod for it making your project dependent on it and deploying both .cods.  note that you can do the work on an lcdui Image and then make a RIM Bitmap and use the setARGB method to get the data in there.

 

if you want something quick and dirty (lightweight code-wise, doesn't do anti-aliasing or anything), you could use this function I whipped up.  just finds the polar coordinates of each pixel, adds an amount of RADIANS (not degrees, use a conversion if necessary), and moves it.

 

         public static Bitmap rotateImage(Bitmap oldB, int angle) throws Exception
        {
            int w = oldB.getWidth();
            int h = oldB.getHeight();
            double angRad = (angle%360)*(Math.PI/180);
            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 axisX = w/2;
            int axisY = h/2;
            
            for(int x = 0; x < oldD.length; x++){
                int oldX = x%w;
                int oldY = x/w;
                int op = oldX-axisX;
                int adj = oldY-axisY;
                double oldT = MathUtilities.atan2(op, adj);
                double rad = Math.sqrt((op*op)+(adj*adj));
                double newT = oldT+angRad;
                int newX = (int)(rad*Math.sin(newT))+axisX;
                int newY = (int)(rad*Math.cos(newT))+axisY;
                if(newX<0||newY<0||newX>=w||newY>=h){
                    newD[x] = 0x00000000;
                }else{
                    newD[x] = oldD[(newY*w)+newX];
                }
            }
            
            newB.setARGB(newD, 0, w, 0, 0, w, h);
            return newB;
        }

New Developer
Posts: 23
Registered: ‎12-29-2008
My Device: Not Specified

Re: How to rotate an image?

my bad, posted the "degrees" version.  if you could do it in radians without the conversion things would be nicer.  also, if you short circuit the cases that are increments of 90 degrees, that would improve performance.

 

second NOTE: it clips at the original image size.  this is just because of the app I was using it in, you could pad the images before hand if necessary.

New Developer
Posts: 23
Registered: ‎12-29-2008
My Device: Not Specified

Re: How to rotate an image?

got some suggestions, here's an improved version 2.  gotta love the forums, thanks guys

 

         public static Bitmap rotateImage(Bitmap oldB, int angle) throws Exception
        {
            int w = oldB.getWidth();
            int h = oldB.getHeight();
            double angRad = (angle%360)*(Math.PI/180);
            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 axisX = w/2;
            int axisY = h/2;
            
            for(int x = 0; x < oldD.length; x++){
                int oldX = x%w;
                int oldY = x/w;
                int op = oldX-axisX;
                int adj = oldY-axisY;
                double oldT = MathUtilities.atan2(op, adj);
                double rad = Math.sqrt((op*op)+(adj*adj));
                double newT = oldT+angRad;
                int newX = (int)MathUtilities.round((rad*Math.sin(newT))+(double)axisX);
                int newY = (int)MathUtilities.round((rad*Math.cos(newT))+(double)axisY);
                if(newX<0||newY<0||newX>=w||newY>=h){
                    newD[x] = 0x00000000;
                }else{
                    newD[x] = oldD[(newY*w)+newX];
                }
            }
            
            newB.setARGB(newD, 0, w, 0, 0, w, h);
            return newB;
        }

Developer
Posts: 33
Registered: ‎05-08-2009
My Device: Not Specified

Re: How to rotate an image?

I just used the above method for both clockwise and anticlockwise rotation. Everything is fine except that the quality of the image and also the size gets decremented for every rotation. Actually i had an rectangular image which became completely circular after the rotation of 180 degrees in steps 5. Is it the limitation? Or is it the problem due to type-casting(double-int)? Is there any way to overcome this issue?. Thanks. 
New Developer
Posts: 23
Registered: ‎12-29-2008
My Device: Not Specified

Re: How to rotate an image?

The algorithm throws out any pixels that are rotated off of the initial square.  You either need to account for this and put transparent corners around a circular image or re-write the algorithm to make the image bigger when it gets rotated.

 

As for the quality, I keep the same source image and rotate it once each time.  I do not rotate and image and then rotate it again, because there is a known degradation of quality.

 

This is just a quick and dirty way of doing it, there's gotta be a better way out there, but it served my purposes (small images, one rotation each)

Developer
Posts: 33
Registered: ‎05-08-2009
My Device: Not Specified

Re: How to rotate an image?

Thanks for the suggestion. Will try it out and let you know
New Developer
Posts: 31
Registered: ‎09-24-2008
My Device: Not Specified

Re: How to rotate an image?

hi,

 

thanks for the code..

 

ur posted code work fine.

but it take too much time for me..

 

how can i improve speed ...

 

waiting for ur reply..

 

thanks

 

Ashish.