IAmGroot
IAmGroot

Reputation: 13855

JavaME Resize Image

I have a byte[] that stores my image.

Now I want to be able to scale it and store it in another byte[].

I know how to scale Bitmaps, and can convert my from byte[] to Bitmap. But I cant get the scaled Bitmap back into a byte[].

Blackberries EncodedImage thumbnail = image.scaleImageToFill(50, 50); does nothing.

Im trying to create a thumbnail of 50x50. doesnt have to be exact. That is stored in a byte[].

How do I resize an image stored in a byte[], and keep it in a new byte[].

 byte[] imageTaken;
 //Create thumbnail from image taken
 EncodedImage image = EncodedImage.createEncodedImage(imageTaken, 0, -1);
 image.getBitmap();
 EncodedImage thumbnail = image.scaleImageToFill(50, 50);           
 byte[] thumbArray = thumbnail.getData();

Upvotes: 1

Views: 458

Answers (1)

Rince Thomas
Rince Thomas

Reputation: 4158

try this code

bitmap = resizeImage(your image, 50,50);


 private static EncodedImage resizeImage(EncodedImage image, int width, int height) {
        if (image == null) {
          return image;
        }

        //return if image does not need a resizing
        if (image.getWidth() <= width && image.getHeight() <= height) {
          return image;
        }

        double scaleHeight, scaleWidth;
        if (image.getWidth() > width && image.getHeight() > height) {  //actual image is bigger than scale size
          if (image.getWidth() > image.getHeight()) {  //actual image width is more that height then scale with width
            scaleWidth = width;
            scaleHeight = (double)width / image.getWidth() * image.getHeight();
          } else { //scale with height
            scaleHeight = height;
            scaleWidth = (double)height / image.getHeight() * image.getWidth();
          }
        } else if (width < image.getWidth()) { //scale with scale width or height
          scaleWidth = width;
          scaleHeight = (double)width / image.getWidth() * image.getHeight();
        } else {
          scaleHeight = height;
          scaleWidth = (double)height / image.getHeight() * image.getWidth();
        }
        int w = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP((int)scaleWidth));
        int h = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP((int)scaleHeight));
        return image.scaleImage32(w, h);
      } 

Then convert the bitmap to Bytes -

JPEGEncodedImage encoder=JPEGEncodedImage.encode(bitmap,100);
                        byte[] array=encoder.getData();
                        int length=array.length;
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length);
                        Base64OutputStream base64OutputStream = new Base64OutputStream( byteArrayOutputStream );
                         try{
                                         base64OutputStream.write( array, 0, length );
                                         base64OutputStream.flush();
                                         base64OutputStream.close();


                                     }
                                        catch (IOException ioe){
                                        //Dialog.alert("Error in encodeBase64() : "+ioe.toString());
                                        System.out.println("Error in encodeBase64() : "+ioe.toString());

                                     }
                                     try {
                                        byte[]  data = Base64InputStream.decode(byteArrayOutputStream.toString());

                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

Upvotes: 6

Related Questions