2011-11-10 2 views
2

이미지?를 저장하는 방법? Pls는 나를 도와 .... 어떤 인코더가 전화 memory.Is에 J2ME 응용 프로그램 에서 이미지를 저장 또는 바이트 배열로 변환 할에서 j2me s40의 전화 메모리에 이미지를 저장하는 방법? 로컬로 application.I에 저장됩니다

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

나는이 코드를 시도했다. 하나의 포맷되지 않은 파일이 불량 이미지 폴더에 저장되면 그 파일 크기는 0.0KB이다. 생각해 보면, 이미지를 읽을 수 없다 ............

+0

E에서 이미지 저장을 시도하십시오., E :는 Nokia Device에있는 휴대 전화의 SD 카드입니다. – Lucifer

+0

고마워, 알았다. pls는 링크 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}; 
} 

이제이 클래스 .IN here에서 PNG.java 클래스를 다운로드 우리는이 :

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

첫 번째의 int

너비와 이미지의 높이이며, 바이트 배열 순서대로 : 빨간색 알파, , 녹색 및 파랑. 너비와 높이가 매우 간단합니다. + 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 파일 연결을 통해 파일에 이미지를 렌더링하십시오.
그런 다음, here에서 com.encoder.jpg을 다운로드 포맷 JPEG로 이미지를 저장하고자하는 경우 :

//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는 정말 멋진 SW 조각이며 작동합니다. 리소스가 제한적인 장치에 적합합니다. 그러나 Sun의 JIMI 라이브러리를 기반으로하고 있으며 현재 Oracle이 소유하고 있습니다. 오라클의 라이센스 조건은 다소 관대하지만 휴대 전화와 같은 임베디드 장치에서의 사용은 거부됩니다. 귀하의 상황에 따라, 이것은 막후 스러울 수도 있습니다.

관련 문제