2011-11-10 75 views
2

圖像保存在本地在application.I想挽救J2ME應用程序 圖像電話memory.Is有任何編碼器或轉換字節數組?如何保存呢?請幫助我....在j2me中如何將圖像保存在手機內存中以保存s40?

 try { 


     String url=System.getProperty("fileconn.dir.photos")+"model0_0.jpg"; 

     FileConnection fc=(FileConnection)Connector.open(url,Connector.READ_WRITE); 
     if(!fc.exists()) { 

      fc.create(); 
     }else { 
      // return; 
     } 


     OutputStream os=fc.openOutputStream(); 
     int iw=galleryImage.getWidth();int ih=galleryImage.getHeight(); 
     rawInt=new int[iw*ih]; 
     galleryImage.getRGB(rawInt,0,iw,0,0,iw,ih); 
     ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
     for(int i=0;i<rawInt.length;i++) 
      baos.write(rawInt[i]); 
     byte byteData[]=baos.toByteArray(); 
     baos.close(); 
     ByteArrayInputStream b_stream=new ByteArrayInputStream(byteData); 
     int i=0; 
     /*while((i=b_stream.read())!=-1) { 
      os.write(i); 
     }*/ 

     for(i=0;i<content.length;i++) { 
      os.write(b_stream.read()); 
     } 

     //os.write(byteData); 
     os.flush(); 
     os.close(); 
     System.out.println("\n\nImage Copied..\n"); 

     fc.close(); 

    } catch (IOException e) { 
     //System.out.println("image not read for gallery"); 
     e.printStackTrace(); 
    } 
    catch(java.lang.IllegalArgumentException iae){iae.printStackTrace();} 
    catch(Exception e){e.printStackTrace();} 

我試過這個code.When一個Unformatted文件存儲在defaut圖像文件夾中。那個文件大小是0.0KB.I認爲,圖像不被讀取............

+0

嘗試在E:中保存圖像,E:是諾基亞設備中手機的SD卡。 – Lucifer

+0

謝謝,我明白了。請通過鏈接https://github.com/Pash237/j2me-JPEG-library並下載com文件夾文件,並導入我的應用程序,並改變了幾行。它的工作很好....... – ranjith

回答

1

如果你有一個來自圖像的RGBA數據,您可以在保存之前對其進行編碼,因此請根據您的目的尋找合適的編碼器。您可以對所有j2me程序使用png格式。首先,您將從圖像中獲得RGBA數據:

/** 
    * Gets the channels of the image passed as parameter. 
    * @param img Image 
    * @return matrix of byte array representing the channels: 
    * [0] --> alpha channel 
    * [1] --> red channel 
    * [2] --> green channel 
    * [3] --> blue channel 
    */ 

public byte[][] convertIntArrayToByteArrays(Image img) { 
int[] pixels = new int[img.getWidth() * img.getHeight()]; 
img.getRGB(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), 
      img.getHeight()); 

// separate channels 
byte[] red = new byte[pixels.length]; 
byte[] green = new byte[pixels.length]; 
byte[] blue = new byte[pixels.length]; 
byte[] alpha = new byte[pixels.length]; 

for (int i = 0; i < pixels.length; i++) { 
    int argb = pixels[i]; 
    //binary operations to separate the channels 
    //alpha is the left most byte of the int (0xAARRGGBB) 
    alpha[i] = (byte) (argb >> 24); 
    red[i] = (byte) (argb >> 16); 
    green[i] = (byte) (argb >> 8); 
    blue[i] = (byte) (argb); 
} 

return new byte[][]{alpha, red, green, blue}; 
} 

現在從here下載PNG.java類。在這個類,我們有:

toPNG(int,int,byte[],byte[],byte[],byte[]) 

第一整數是寬度和圖像的高度,字節陣列是爲了:α,紅,綠色和...藍色。寬度和高度非常簡單:+ getWidth():int和+ getHeight():來自Image對象的int(和你一樣),其他的可以通過** convertIntArrayToByteArrays **獲得:

byte[][] rgba = convertIntArrayToByteArrays(galleryImage); 
byte[] encodeImage = toPNG(galleryImage.getWidth(),galleryImage.getHeight(),rgba[0] ,rgba[1] ,rgba[2],rgba[3]); 

現在您可以保存enco通過文件連接將文件刪除。
如果你喜歡保存格式JPEG圖像,從here下載com.encoder.jpg,則:

//Create MediaProcessor for raw Image 
MediaProcessor mediaProc = GlobalManager.createMediaProcessor("image/raw"); 
//Get control over the format 
ImageFormatControl formatControl = (ImageFormatControl) 
     mediaProc.getControl("javax.microedition.amms.control.ImageFormatControl"); 
//Set necessary format 
formatControl.setFormat("image/jpeg"); 

Refrences:
stackoverflow

import com.encoder.jpg.*; 

//your input image 
Image image = Image.createImage(128, 128); 

JPGEncoder encoder = new JPGEncoder(); 
int quality = 65; 
byte[] encodedImage = encoder.encode(image, quality); 

//now save or send encoded jpeg image 

最後,你可以從jsr234使用MediaProcessor java-n-me

+0

謝謝@hasanghaforian放置所有可用的解決方案在一個職位。我從JPGEncoder獲得了非常好的性能,它非常優化,您還可以定義影響文件大小的編碼質量。 PNG.java在大文件上給出了內存不足的例外情況,所以它可能不適合小堆大小的手機。 – Ajibola

2

JPGEncoder是一個非常好的軟件,它的工作很適合資源受限的設備。但是,它基於Sun現在擁有的JIMI庫。甲骨文的許可條款有些寬容,但他們拒絕在手機等嵌入式設備上使用。根據你的情況,這可能是一個showstopper。

相關問題