2014-02-07 7 views
1

파일 선택기로 이미지를로드 한 다음 이미지를 지정된 크기로 조정하여 DB에 저장하는 방법이 있습니다. 나는 com.sun.image.codec.jpeg.JPEGCodec와 JPEGImageEncoder를 사용했고 나는 Imagebe로 변경해야한다. 넷빈즈 7.4 IDE에서 사용할 수 없기 때문이다. 이게 내가 가진거야.com.sun.image. *를 ImageIO로 변경하는 방법

FileInputStream fis = new FileInputStream(image); 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
byte[] buf = new byte[1024]; 
for (int readNum; (readNum = fis.read(buf)) != -1;) { 
    bos.write(buf, 0, readNum); 
} 
person_image = bos.toByteArray(); 
ImageIcon imageIcon = new ImageIcon(person_image); 
Image img = imageIcon.getImage(); 
Image imageResize = img.getScaledInstance(lbl_fotoPaciente.getWidth(), lbl_fotoPaciente.getHeight(), Image.SCALE_SMOOTH); 
ImageIcon imageIconResize = new ImageIcon(imageResize); 
int resizeWidth = imageIconResize.getIconWidth(); 
int resizeHeight = imageIconResize.getIconHeight(); 

Panel p = new Panel(); 
BufferedImage bi = new BufferedImage(resizeWidth, resizeHeight, 
     BufferedImage.TYPE_INT_RGB); 

Graphics2D big = bi.createGraphics(); 
big.drawImage(imageResize, 0, 0, p); 

ByteArrayOutputStream os = new ByteArrayOutputStream(); 
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); 
encoder.encode(bi); 
person_image = os.toByteArray(); 
lbl_fotoPaciente.setIcon(new ImageIcon(imageResize)); 

어떻게 ImageIO로 변경할 수 있습니까? 감사.

답변

3

당신은 단순히 함께 마지막 라인을 대체 할 수

... 
ByteArrayOutputStream os = new ByteArrayOutputStream(); 
ImageIO.write(bi, "JPEG", os); 
person_image = os.toByteArray(); 
... 
+0

덕분에, 그것을 작동합니다. 좋은 하루 되세요. –