2010-05-19 61 views
0

我正在使用下面的代碼來調整大小並將文件保存到黑莓設備中。圖像縮放後,我嘗試將圖像文件寫入設備。但它提供了相同的數據。 (圖像的高度和寬度是相同的)。我必須做重新縮放的圖像文件。任何人都可以幫助我?黑莓圖像重新調整和寫入重新調整後的圖像文件

類ResizeImage擴展MainScreen實現FieldChangeListener { 私人字符串路徑= 「文件:///SDCard/BlackBerry/pictures/test.jpg」; 私人ButtonField btn; ResizeImage() { btn = new ButtonField(「Write File」); btn.setChangeListener(this); add(btn); } 公共無效fieldChanged(場場,INT上下文) { 如果(場== BTN) { 嘗試 {
的InputStream的inputStream = NULL; //獲取文件連接 FileConnection fileConnection =(FileConnection)Connector.open(path); if(fileConnection.exists()) inputStream = fileConnection.openInputStream(); // byte data [] = inputStream.toString()。getBytes();

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        int j = 0; 
        while((j=inputStream.read()) != -1) { 
        baos.write(j); 
        } 
        byte data[] = baos.toByteArray();     
        inputStream.close(); 
        fileConnection.close(); 

        WriteFile("file:///SDCard/BlackBerry/pictures/org_Image.jpg",data);   


        EncodedImage eImage = EncodedImage.createEncodedImage(data,0,data.length);        
        int scaleFactorX = Fixed32.div(Fixed32.toFP(eImage.getWidth()), Fixed32.toFP(80)); 
        int scaleFactorY = Fixed32.div(Fixed32.toFP(eImage.getHeight()), Fixed32.toFP(80)); 
        eImage=eImage.scaleImage32(scaleFactorX, scaleFactorY); 

        WriteFile("file:///SDCard/BlackBerry/pictures/resize.jpg",eImage.getData()); 

        BitmapField bit=new BitmapField(eImage.getBitmap());      
        add(bit); 

       }  
      } 
      catch(Exception e) 
      { 
       System.out.println("Exception is ==> "+e.getMessage()); 
      } 

     } 
    } 


    void WriteFile(String fileName,byte[] data) 
    { 
     FileConnection fconn = null; 
     try 
     { 
      fconn = (FileConnection) Connector.open(fileName,Connector.READ_WRITE); 
     } 
     catch (IOException e) 
     { 
      System.out.print("Error opening file"); 
     } 

     if (fconn.exists()) 
     try 
     { 
      fconn.delete(); 
     } 
     catch (IOException e) 
     { 
      System.out.print("Error deleting file"); 
     } 
     try 
     { 
      fconn.create(); 
     } 
     catch (IOException e) 
     { 
      System.out.print("Error creating file"); 
     } 
     OutputStream out = null; 
     try 
     { 
      out = fconn.openOutputStream(); 
     } 
     catch (IOException e) { 
      System.out.print("Error opening output stream"); 
     } 

     try 
     { 
      out.write(data); 
     } 
     catch (IOException e) { 
      System.out.print("Error writing to output stream"); 
     } 

     try 
     { 
      fconn.close(); 
     } 
     catch (IOException e) { 
      System.out.print("Error closing file"); 
     } 
    } 

}

回答

0

退房getScaledHeight()和getScaledWidth()在EncodedImage。

這是一個骯髒的RIM技巧。

如果您將getBitmap()的位圖從getHeight()和getWidth()中剝離出來。

然後,如果您想將縮放的圖像另存爲jpeg,則需要重新編碼。

例如

Bitmap scaledBMP = scaledEI.getBitmap(); 
JPEGEncodedImage finalJPEG = JPEGEncodedImage.encode(scaledBMP, 80); // int arg is quality 
raw_media_bytes = finalJPEG.getData(); 
raw_length = finalJPEG.getLength(); 
raw_offset = finalJPEG.getOffset(); 

// don't forget to use the length and offset info, because getData() is 
// not guaranteed to work by itself. 

AFAIK,預5.0至少沒有在天然-JPEG縮放W/O轉換爲位圖。