JavaME调整图像大小

Doo*_*ght 1 blackberry image java-me

我有一个byte[]存储我的图像.

现在我希望能够扩展它并将其存储在另一个中byte[].

我知道如何扩展Bitmaps,并可以将我转换byte[]Bitmap.但是我不能将缩放的Bitmap重新变成一个byte[].

黑莓 EncodedImage thumbnail = image.scaleImageToFill(50, 50);什么都不做.

我试图创建一个50x50的缩略图.不一定要准确.它存储在byte []中.

如何调整存储在byte []中的图像的大小,并将其保存在新的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();
Run Code Online (Sandbox Code Playgroud)

Sig*_*are 6

试试这段代码

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);
      } 
Run Code Online (Sandbox Code Playgroud)

然后将位图转换为字节 -

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();
                                    }
Run Code Online (Sandbox Code Playgroud)