문제

I have a method to load an image with a filechooser and then it resize the image to a specified size to store it in the DB. i used com.sun.image.codec.jpeg.JPEGCodec and JPEGImageEncoder and i need to change it to ImageIO because i cant use it on netbeans 7.4 IDE. this is what i have.

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

How can i change it to ImageIO? thanks.

도움이 되었습니까?

해결책

You can simply replace the last lines with:

...
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bi, "JPEG", os);
person_image = os.toByteArray();
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top