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: 28
Registered: ‎12-18-2008
My Device: Not Specified

Reduce size of images...

hi

 

I m sending images as attachments in my BlackBerry application. I m sending those images which are taken from the BlackBerry camera. Those images have an original size of about 400KB. Therefore it takes some time to attach and send the images. Is there a way to reduce the size of the images by image compression or scaling down the image? 

 

Thanks

 

Developer
Posts: 28
Registered: ‎12-18-2008
My Device: Not Specified

Re: Reduce size of images...

Do anyone of you know how to use the ZLibInputStream and ZLibOutputStream classes?

 

What I have is a byte array of the image. I want to somehow campress it so that the image size is reduced.

I have tried like this. Is this possible? This throws an "IO Exception: invalid block type"

......

.....

InputStream input = FileConnection.openInputStream();     

ZLibInputStream z = new ZLibInputStream(input, true, 1024);


                                        Databuffer buffer = new DataBuffer();
                                        byte[] temp = new byte[1024];
               
                                        for( ;; ) {
                                                int bytesRead = z.read( temp );
                                                if( bytesRead == -1 ) {
                                                break;
                                        }
                                                buffer.write( temp, 0, bytesRead );
                                        }
                                       
                                        input.close();
                                        z.close();

 

................

................

//attaching to email

.......

mp.addBodyPart(new SupportedAttachmentPart(mp,"image/jpg",imageName+".jpg",buffer.getArray()));

........

 

 

Can you please help me on this? Is it possible reduce the image (here i'm reading from an jpg image) sizes using the above classes?

 

Thanks

Developer
Posts: 16,575
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Reduce size of images...

you can change the size of images using EncodedImage.scaleImage32. take care of the method using Fix32 ints.

you won't get any size reduction when you try to compress a jpg using ZLib (or any other compression) as jpg is already compressed.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 28
Registered: ‎12-18-2008
My Device: Not Specified

Re: Reduce size of images...

I tried that earlier also using both scaleImage32() method and setScale() but noe of them didnt have any impact on the image size. See below.

I have used following code for that. Is there something wrong here? I don't know why this is not working.

 

//read the image to the DataBuffer buffer as shown in the previous code snippet

 

EncodedImage ec = EncodedImage.createEncodedImage(buffer.getArray(), 0, buffer.getArray().length,"image/jpg");
                       //ec.setScale(10);
                       EncodedImage ec2 = ec.scaleImage32(5, 5);                                                   
                       .......                
                      //attaching to the mail                         
                      mp.addBodyPart(new SupportedAttachmentPart(mp,"image/jpg",imageName+".jpg",ec2.getData()));

 

                    ......

 

 

Thanks..

BlackBerry Development Advisor
Posts: 15,016
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Reduce size of images...

scaleImage reduces the scale of the image (size displayed).  It won't have an affect on the file size of an image.  The BlackBerry API set doensn't have any built in APIs to reduce the file size of an image.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Contributor
Posts: 31
Registered: ‎11-14-2012
My Device: Blackberry 9780

Re: Reduce size of images...

Hi I've been following the thread here: http://supportforums.blackberry.com/t5/Java-Development/How-to-Compress-the-Bitmap/m-p/1556773/highl... and others. Is this still not possible? So you're saying... even with scaleImage32, nothing will happen to the file size? 

I actually asked that very same question here...

http://supportforums.blackberry.com/t5/Java-Development/Resizing-a-Bitmap-using-scaleImage32-instead...

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

Re: Reduce size of images...