2010-06-11 100 views

回答

0

我不知道它是否是你需要的,但你可以從全尺寸JPEG文件中取出嵌入式縮略圖。在我的j2me應用程序中,我以這種方式顯示了手機庫。

private final static int STOP_AT_BYTE = 8192; //how far to search for thumbnail 
private final static int THUMB_MAX_SIZE = 16284; 

private Image getThumbnailFromStream(InputStream str, long fileSize) 
{ 
    byte[] tempByteArray = new byte[THUMB_MAX_SIZE]; // how big can a thumb get. 
    byte[] bytefileReader = {0}; // lazy byte reader 
    byte firstByte,secondByte = 0; 
    int currentIndex = 0; 

    int currByte = 0; 

    try { 
     str.read(bytefileReader); 
     firstByte = bytefileReader[0]; 
     str.read(bytefileReader); 
     secondByte = bytefileReader[0]; 

     currByte += 2; 

     if ((firstByte & 0xFF) == 0xFF && (secondByte & 0xFF) == 0xD8) { //if this is JPEG 
      byte rByte = 0; 
      do { 
       while (rByte != -1 && currByte < fileSize) { 
        str.read(bytefileReader); 
        rByte = bytefileReader[0]; 
        currByte++; 
       } 

       str.read(bytefileReader); 
       rByte = bytefileReader[0]; 
       currByte++; 

       if (currByte > STOP_AT_BYTE) { 
        return null; 
       } 
      } while ((rByte & 0xFF) != 0xD8 && currByte < fileSize); // thumb starts 

      if (currByte >= fileSize) { 
       return null; 
      } 

      tempByteArray[currentIndex++] = -1; 
      tempByteArray[currentIndex++] = rByte; 
      rByte = 0; 

      do { 
       while (rByte != -1){ 
        str.read(bytefileReader); 
        rByte = bytefileReader[0]; 
        tempByteArray[currentIndex++] = rByte; 
       } 

       str.read(bytefileReader); 
       rByte = bytefileReader[0]; 
       tempByteArray[currentIndex++] = rByte; 
      } while ((rByte & 0xFF) != 0xD9); // thumb ends 

      tempByteArray[currentIndex++] = -1; 
      Image image = Image.createImage(tempByteArray, 0, currentIndex-1); 
      tempByteArray = null; 

      return image; 
     } 
    } catch (Throwable e) { 
     //error 
    } 

    return null; 
} 
+0

Hi Pavel, 感謝您的回覆。你可以通過從全尺寸JPEG文件中提取嵌入的縮略圖來詳細說明你的意思嗎?現在,我需要一種方法來查找縮略圖的路徑,或者在我的代碼中壓縮庫中的全尺寸圖像。任何建議將不勝感激。 謝謝, 阿希什。 – 2010-06-12 05:25:21

+0

請參閱http://www.media.mit.edu/pia/Research/deepview/exif.html。 您只需要逐字節地讀取全尺寸圖像,直到找到0xFFD8標記。這將是嵌入縮略圖的開始。 – 2010-06-13 20:08:42

+0

我附上我的代碼:) – 2010-06-13 20:17:08