2012-03-14 238 views
1

我有一个byte[]存储我的图像。JavaME调整图像大小

现在我想能够将其缩放并将其存储在另一个byte[]中。

我知道如何缩放Bitmap s,并且可以将我从byte[]转换为Bitmap。 但我不能得到缩放的位图返回到byte[]

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

我试图创建一个50x50的缩略图。不一定是确切的。这存储在一个字节[]中。

如何调整存储在字节[]中的图像的大小,并将其保存在新的字节[]中。

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(); 
+2

这里的答案:http://stackoverflow.com/questions/6650998/blackberry-get -image-data-from-bitmap – rosco 2012-03-14 12:59:55

+0

@rosco。谢谢,但需要另一个java代码文件。可能类似于下面的答案。此外链接已损坏。 – Doomsknight 2012-03-14 14:56:28

回答

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); 
     } 

然后将位图转换为字节 -

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(); 
            } 
+0

我该如何回到'byte []'? – Doomsknight 2012-03-14 12:41:50

+0

我更新了答案。检查它 – Signare 2012-03-14 12:59:40

+1

优秀的东西,非常感谢。我把你的BitmaptoArray放到它自己的方法中。并使用我的旧代码来调整位图的大小。完美的作品。再次感谢:) – Doomsknight 2012-03-14 14:21:09