2017-08-22 80 views
0

將inputStream轉換爲bufferImage存在問題。 BufferedImage crfPersonImg始終爲空,異常發生在行柵格柵格= reader1.readRaster(0,null);。這是到目前爲止我的代碼javax.imageio.IIOException:不是JPEG流(開頭爲:0x0003,預計SOI:0xffd8)

//code for getting ByteArray From Blob column in Oracle DB. 
    List<Object[]> userRoleList = commonDao.findByNativeSQL(selectSQL 
      , new Object[] {examCd, examPeriod, fromMemNo, toMemNo}); 
    for(Object[] row : userRoleList){ 
     CertificateDtlsVO cdObj = new CertificateDtlsVO();   
     cdObj.setExmCd(Integer.parseInt(row[0].toString())); 
     cdObj.setExmDsc(row[1].toString()); 
     cdObj.setMemNo(row[2].toString()); 
     cdObj.setMemNm(row[3].toString()); 
     cdObj.setCertNo(row[4].toString()); 
     cdObj.setCertDt(row[5].toString()); 
     cdObj.setPrtNo(row[6].toString()); 
     cdObj.setSylCd(row[7].toString()); 
     cdObj.setNumOfTime(Integer.parseInt(row[8].toString())); 
     cdObj.setMemType(row[9].toString()); 
     cdObj.setClassGrade(row[10].toString()); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(baos); 
     oos.writeObject(row[11]);   
     cdObj.setPhoto(baos.toByteArray()); 
     baos = new ByteArrayOutputStream(); 
     oos = new ObjectOutputStream(baos); 
     oos.writeObject(row[12]); 
     cdObj.setSign(baos.toByteArray()); 
     cdObj.setPhotoBuffImg(ImageIO.read(new ByteArrayInputStream(baos.toByteArray()))); 
     oos.flush(); 
     oos.close(); 
     memCertfDtlsList.add(cdObj); 
    } 

//代碼轉換爲字節數組BufferImage

InputStream in = new ByteArrayInputStream(photo);  
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG"); 
    ImageReader reader1 = null; 
    while (readers.hasNext()) { 
     reader1 = readers.next(); 
     if (reader1.canReadRaster()) { 
      break; 
     } 
    } 

    //Stream the image file 
    ImageInputStream input = ImageIO.createImageInputStream(in); 
    reader1.setInput(input); 

    //Read the image raster 
    Raster raster = reader1.readRaster(0, null); 

    //Create a new RGB image 
    BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), 
      BufferedImage.TYPE_4BYTE_ABGR); 

    //Fill the new image with the old raster 
    bi.getRaster().setRect(raster); 

    BufferedImage crfPersonImg = ImageIO.read(in); 

javax.imageio.IIOException:爲0x0003,預計SOI:不是一個JPEG視頻流(開頭爲: 0xffd8) 在com.twelvemonkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.streamInit(JPEGSegmentImageInputStream.java:247) 在com.twelvemonkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.fetchSegment(JPEGSegmentImageInputStream.java:71) 在融爲一體。十二monkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.repositionAsNecessary(JPEGSegmentImageInputStream.java:267)

樣本圖像sample Image

+0

我試圖直接使用'ImageIO.read()'和'ImageReader.readRaster()'來讀取文件,並且在兩種情況下都能正常工作。我不明白你爲什麼需要這兩個,但他們工作。所以,我會懷疑問題出在您沒有顯示的代碼中。喜歡什麼是照片?一個'byte'數組?它是如何填充的? – haraldK

+0

** InputStream in = new ByteArrayInputStream(photo)**我用來將photo byte []轉換爲inputStream。 [這是我的第二個圖像樣本](http://fotoforensics.com/analysis.php?id=70c5fd394b5e086bdc9b74db7944cc6c222c7818.8021&fmt=orig) –

+0

@haraldK ** InputStream in = new ByteArrayInputStream(photo)**我用於將照片字節[]轉換爲inputStream。 [這是我的第二個圖像樣本](http://fotoforensics.com/analysis.php?id=70c5fd394b5e086bdc9b74db7944cc6c222c7818.8021&fmt=orig)和[Byte Array](https://ufile.io/7iqst)。我通過dataBase oracle Blob列填充。它的jpeg fomate。 –

回答

0

由於@haraldK,通過改變從BLOB列得到字節數組的代碼解決我的問題。

List<Object[]> userRoleList = commonDao.findByNativeSQL(selectSQL 
      , new Object[] {examCd, examPeriod, fromMemNo, toMemNo}); 
    for(Object[] row : userRoleList){ 
     CertificateDtlsVO cdObj = new CertificateDtlsVO();   
     cdObj.setExmCd(Integer.parseInt(row[0].toString())); 
     cdObj.setExmDsc(row[1].toString()); 
     cdObj.setMemNo(row[2].toString()); 
     cdObj.setMemNm(row[3].toString()); 
     cdObj.setCertNo(row[4].toString()); 
     cdObj.setCertDt(row[5].toString()); 
     cdObj.setPrtNo(row[6].toString()); 
     cdObj.setSylCd(row[7].toString()); 
     cdObj.setNumOfTime(Integer.parseInt(row[8].toString())); 
     cdObj.setMemType(row[9].toString()); 
     cdObj.setClassGrade(row[10].toString()); 
     InputStream photoBlob = ((Blob) row[11]).getBinaryStream(); 
     InputStream signBlob = ((Blob) row[12]).getBinaryStream(); 
     cdObj.setPhoto(IOUtils.toByteArray(photoBlob)); 
     cdObj.setSign(IOUtils.toByteArray(signBlob)); 
     memCertfDtlsList.add(cdObj); 
    }