加载和调整图像大小

时间:2014-06-11 11:05:06

标签: java image swing graphics

我正在Java Swing中加载一个图像并希望在JPanel中显示它。我面临的问题是,尽管图像被加载,但它没有在JPanel中显示..我可以说图像已成功加载,因为它显示了我标签中加载图像的正确路径..

这是我的代码..

private static final int IMG_WIDTH = 120;
private static final int IMG_HEIGHT = 120;
JLabel label;
ImageIcon photo;
WritableRaster raster;
DataBufferByte data;
File image;

JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
chooser.setCurrentDirectory(image);
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);

int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
    image = chooser.getSelectedFile();
    jLabel8.setText(image.getAbsolutePath());
    try {
        BufferedImage originalImage = ImageIO.read(image);
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        BufferedImage resizeImageJpg = resizeImage(originalImage, type);
        photo = new ImageIcon(toImage(resizeImageJpg));
        jPanel2.removeAll();
        label = new JLabel("", photo, JLabel.CENTER);
        label.setIcon(photo);
        jPanel2.add(label);
        setVisible(true);
            //converting buffered image to byte array
        raster = resizeImageJpg.getRaster();
        data = (DataBufferByte) raster.getDataBuffer();

     } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        repaint();
        chooser.setCurrentDirectory(image);
    }

  public Image toImage(BufferedImage bufferedImage) {
    return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}


private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();

    return resizedImage;
}

1 个答案:

答案 0 :(得分:1)

首先,忘记toImage()方法,BufferedImage已扩展Image类。

然后尝试使用

    try {
        BufferedImage originalImage = ImageIO.read( image );
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        BufferedImage resizeImageJpg = resizeImage( originalImage, type );

        //forget the toImage() method, BufferedImage already extends the Image class.
        photo = new ImageIcon( resizeImageJpg );
        //And by the way, if you remove components and add new ones, use validate() instead.

        //And don't remove the JLabel, just change the icon, it will repaint automatically.
        label.setIcon(photo);
    }
    catch ( java.io.IOException iOException ) { 
        System.out.println(e.getMessage());
    }

对于调整大小的BufferedImage,使用AffineTransform将图像缩放到新的大小。

private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);

    //Calculate de scale ratio;
    double scaledDx = ((IMG_WIDTH*1.0d)/originalImage.getWidth());
    double scaledDy = ((IMG_HEIGHT*1.0d)/originalImage.getHeight());

    AffineTransform resizeAffine = AffineTransform.getScaleInstance( scaledDx, scaledDy );
    BufferedImageOp buffResized = new AffineTransformOp( resizeAffine, null );

    Graphics2D g2 = resizedImage.createGraphics();
    g2.drawImage( resizedImage, buffResized, 0, 0 );
    g2.dispose(); //Don't forget to dispose to release resources.
}

我希望我能提供帮助。