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
Posts: 23
Registered: ‎08-02-2008
My Device: BlackBerry Dev Alpha
My Carrier: Bharti Airtel

How to crop image in Blackberry ?

Hi All

 

Is there any method to crop image in blackberry by specifying the co ordinates i.e, I have got topLeft coordinates, then width and height.

 

If anyone knows how to do this please do update, it will be really helpful.

 


Regards
Anand
Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to crop image in Blackberry ?

Look at Bitmap getARGB and setARGB to do this.

New Developer
Posts: 37
Registered: ‎03-02-2009
My Device: Bold 9700
My Carrier: TMO

Re: How to crop image in Blackberry ?

Graphics.drawBitmap(x, y, canvasW, canvasH, bitmap, xOff, yOff);

 

x = X offset within canvas

y = Y offset within canvas

 

xOff = x offset within bitmap

yOff = y offset within bitmap

 

So, if you set xOff and yOff to 10, you will crop off the top and left 10px of the image.  Changing the canvasW and canvasH values will crop off the bottom and right side of the image.

 

So, to draw the entire image:

Graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);

 

To crop by 10px all the way around:

Graphics.drawBitmap(0, 0, bitmap.getWidth()-10, bitmap.getHeight()-10, bitmap, 10, 10);

 

etc...

 

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

Re: How to crop image in Blackberry ?

The option suggested by redbutt can be used to display only part of the image, if that is what you want.  I had assumed that you wanted to create a new image that is only part of the original, which is why I suggested set/getARGB.

Developer
Posts: 23
Registered: ‎08-02-2008
My Device: BlackBerry Dev Alpha
My Carrier: Bharti Airtel

Re: How to crop image in Blackberry ?

Hi Peter


 I have tried using that, but it didn't work properly for for me. I'll describe more for you to get an idea so that we can get a solution.

 

I am doing a rotate operation on a bitmap, then thereafter, I want to crop the image. In that I've tried mentioning the x,y co ordinates for cropping inside the setARGB method, but It gave me IllegalAgruementException, I am pasting the code I tried.

 

private Bitmap rotateImage (Bitmap oldB, double 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;
    }

 

 

Here I tried to specify my X,Y co ordinates on newB.setARGB(newD, 0, w, X, Y, w, h); But the same throws IllegalArguementException on running the App.

 

 

 

Regards
Anand
Developer
Posts: 23
Registered: ‎08-02-2008
My Device: BlackBerry Dev Alpha
My Carrier: Bharti Airtel

Re: How to crop image in Blackberry ?

Hi

 

  If can anyone tell If we can do the rotation more efficiantly, Are there any API's for doing it directly ? What I have used, that mentioned in the above message, it is taking lot of time to rotatae as it is doing pixel by pixel. Do any one know a better solution for rotating the image ?

 

Please do update, If anyone have got an idea about how we can do it more quickly (efficiently).

Regards
Anand
Developer
Posts: 19,629
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to crop image in Blackberry ?

Re cropping, in your code, you have the rotated Bitmap in newD, and it has width of w and height of h.  We could describe this as a Bitmap with top left hand corner at 0,0 and the bottom right at w-1, h-1.

 

Now which parts of this new image do you want to see?

 

For example, if you wanted to crop the outer 10 pixels, then you want to see the image at

10, 10, w-21, h-21

 

With respect to rotation, do you want to allow any rotation.  I have some quick code that does 90, 180, 270 and 360 (wow..... :smileyhappy:) rotations, but looks like you want to do other angles.

Developer
Posts: 23
Registered: ‎08-02-2008
My Device: BlackBerry Dev Alpha
My Carrier: Bharti Airtel

Re: How to crop image in Blackberry ?

Hi Peter, About cropping the image, I have got an image of size say width w and height h And I have got a rectangular region inside the image to crop. But for now I think if I can position it in a way such that the top left corner of the rectangle If I can specify for the bitmap offset then it'll be somewhat resolved. But the problem is that after rotation the image will be such that some of Its corners will be white/cuts, how we can avoid such white spaces coming in the corners. Is there any methods to find the top left corner that it may not contain any White spaces can be avoided ? Also speaking about the rotation efficiency I want to rotate the Image at any given angle eg: 23.14 Hope you can point me in the right direction.
Regards
Anand
Developer
Posts: 155
Registered: ‎02-18-2009
My Device: Not Specified

Re: How to crop image in Blackberry ?

Game programmers are probably the ones that know how to optimize bitmap transformations. See for example http://www.gamedev.net/reference/articles/article811.asp

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

Re: How to crop image in Blackberry ?

[ Edited ]

Good link and idea martijnbrinkers.

 

As an exercise, I implemented Method 1 given by jeff@lorrie.atmos.washington.edu (Jeff Bowden).  The image I used took about 4 seconds to rotate using the algorithm supplied by ananthasivanvk, and about 1.5 using Method 1.  So a lot better but still not fast.  I don't know bresenham's algorithm so haven't tried any other options he suggested. 

 

Few questions from previous post:

 

"how we can avoid such white spaces coming in the corners" - you can't, take any picture and rotate it and see what happens.....  I note in your algorithm you do loop off the extremities too, which I probably wouldn't do.  You should be able to turn the 'white' edges transparent - would that save you cropping? 

 

" Is there any methods to find the top left corner that it may not contain any White spaces can be avoided ?"

I'm sure you can calculate the size and position of a square or rectangle that will have not white parts if you try hard, it is just trigonometry.  You will loose part of the picture of course.