07-30-2012 08:08 AM
Hello Everyone,
i want resize my image . i have tried to do that but not getting correct output .
my image width =1600 && height =1200 but i want to image size like height =100 && width =100.
please help me i have searched in blackberry forum but still
i tried this code
EncodedImage ecimage = EncodedImage.createEncodedImage(rawImage, 0, rawImage.length);
int currentWidthFixed32 = Fixed32.toFP(ecimage.getWidth());
int currentHeightFixed32 = Fixed32.toFP(ecimage.getHeight());
int reqWidth = ecimage.getWidth()/2;
int reqHeight = ecimage.getHeight()/2;
int requiredWidthFixed32 = Fixed32.toFP(reqWidth);
int requiredHeightFixed32 = Fixed32.toFP(reqHeight);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,requiredWidthFixed
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,requiredHeightFix
EncodedImage dstecImage = ecimage.scaleImage32(scaleXFixed32, scaleYFixed32);
please do needful. Thanks in advance
07-30-2012 09:00 AM - edited 07-30-2012 09:09 AM
Hi Coolfish
Follow this code to resize image
parameters need to pass
bitmap_source
width
height
Bitmap.FILTER_BILINEAR
Bitmap.SCALE_TO_FIT
This works perfectly fine in my app.
Ask if you still have any issues.
07-30-2012 09:44 AM
I think the code supplied by shaan_softwaredvlpr is originally from this KB article:
http://supportforums.blackberry.com/t5/Java-Develo
This code is needed for transparent images. If you don't have a transparent image, then you can use the Bitmap.scaleInto() method which is far more efficient.
But for me the big issue here is the size of the image. Do you really need to send a 1600 by 1000 bit image to the BlackBerry and then have the BlackBerry resize it? As a Bitmap, a 1600 * 1000 pixel image will take up about 6 MB for storage. That seems like a waste to me and I would suggest you look at getting a smaller image.
07-31-2012 12:39 AM
07-31-2012 12:49 AM - edited 07-31-2012 12:50 AM
Yeah Peter is right ,
The code i given it to you is actually from KB article.
You can also this code to resize your image
private Bitmap myLogo = new Bitmap(width , height); <original image>.scaleInto(myLogo, Bitmap.FILTER_BILINEAR)
But by using this code what happens , if your original bitmap is having some transparency part . it wil filled by a white background. If you are having bitmap in rectengular or square shape , this will be fine else i will suggest you to use that transparency code.
Thanks
07-31-2012 01:25 AM
Hey Peter Thnaks for replay , as per your question , actually i am taking snap from my blackberry smart phone 9800
and when i am taking sanp it will generate the size of image either 1600*1200 or 1024*768 or 640*480 but that size i dont want to store in my database it will take more size so i want to store as 100* 100 or 50*50 or any size which is less then the default resolution of smart phone. to work around this problem after taking the sanp i am resizing the image and then send to the user and database. please share if you any idea to solve this problem.... thanks in adavance
07-31-2012 01:45 AM
Do you need to resize the image & than save this reduced image size to your database.
For that you need to first resize your bitmap & than by its ByteArray you need to save it.
http://stackoverflow.com/questions/2863277/image-r
Check this link , it may help you.
07-31-2012 01:53 AM - edited 07-31-2012 02:08 AM
Hey Shaan Thanks for replay . now (another way ) i am taking the snap and store to the database and after that i am trying to resize the image . can tell me what wrong in the below code?? and passing jpg or png image path.
private void resizeImage(String imageSrcPath, String imageDestPath,int width, int height){
fileManager = new FileManager();
FileConnection fc = null;
FileConnection fconn = null;
OutputStream imageecOutputStream;
InputStream imageInputStream;
//byte[] buffer = null;
try{
fconn = (FileConnection)Connector.open(imageSrcPath);
if(fconn.exists()){
fconn.setReadable(true);
imageInputStream = fconn.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = imageInputStream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, len);
}
byte[] imageData = baos.toByteArray();
EncodedImage ecimage = EncodedImage.createEncodedImage(imageData, 0, imageData.length);
int currentWidthFixed32 = Fixed32.toFP(ecimage.getWidth());
int currentHeightFixed32 = Fixed32.toFP(ecimage.getHeight());
int reqWidth = ecimage.getWidth()/2;
int reqHeight = ecimage.getHeight()/2;
int requiredWidthFixed32 = Fixed32.toFP(reqWidth);
int requiredHeightFixed32 = Fixed32.toFP(reqHeight);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,requiredWidthFixed
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,requiredHeightFix
EncodedImage dstecImage = ecimage.scaleImage32(scaleXFixed32, scaleYFixed32);
byte[] ecimageBytes = dstecImage.getData();
String subPath = imageDestPath.substring(taskRootPath.length(), imageDestPath.length());
if(subPath.indexOf("/") > 0)
fileManager.createDirectoryRecursive(subPath.subst
fc = fileManager.getFile(imageDestPath);
if(null != fc && fc.exists()){
fc.setWritable(true);
imageecOutputStream = fc.openOutputStream();
imageecOutputStream.write(ecimageBytes, 0, ecimageBytes.length);
imageecOutputStream.flush();
imageecOutputStream.close();
//fconn.delete();
}
imageInputStream.close();
baos.close();
}
}catch(IOException e){
e.printStackTrace();
}
finally
{
try {
if(null != fc)
fc.close();
if(null != fconn)
fconn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
07-31-2012 05:20 AM
When you ask us to tell you what is wrong with some code, can you please also tell us what problem you actually see?
With respect to the code, the following code effectively reads the file in and writes it our again to get it into a byte array. Why not just find out the length of the file, and then create a suitable size byte array and read it all in to that?
fconn = (FileConnection)Connector.open(imageSrcPath);
if(fconn.exists()){
fconn.setReadable(true);
imageInputStream = fconn.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = imageInputStream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, len);
}
byte[] imageData = baos.toByteArray();
07-31-2012 08:11 AM
Hey Peter ,
i will do that by adding code
like
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
int size = (int) fconn.fileSize();
byte[] buffer = new byte[size];
while ((len = imageInputStream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, len);
}
but still not getting size if i am passing 100*100