选择图像,然后从所选图像中选择一个子图像

时间:2014-05-13 21:21:07

标签: java swing awt jfilechooser

在我的Java代码中,我正在尝试从我的目录中浏览图像,然后尝试访问所选图像并从我的图像中捕获一个区域(子图像)。我有一些问题,一旦我选择图像,我无法访问图像(ImageIcon或BufferedImage)。因为,我需要它的信息才能从中获取子图像。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class selectWindow extends JFrame{

// In this GUI project we need just one JPanel inside the JFrame
private JPanel imagePanel;
private JPanel buttonPanel;
private JButton selectImage;
private JLabel imageLabel;
File targetFile;
private ImageIcon image;
static BufferedImage targetImg;
private static final String basePath = "C:\\Users\\mroozbahani3\\Desktop";
private static final int baseSizeX = 900-110;
private static final int baseSizeY = 660;
Rectangle captureRect;
File file;

// Now let's make the constructor 
public selectWindow(){

    // The below code will close the window(JFrame), once we have click on window exit
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //lets define the default size of our window page(main JFrame), where the first value is the x and second one is y;
    setSize(900,660);
    selectImage = new JButton("Select Image");
    selectImage.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            image = selectButtonActionPerformed(e);

        }
    });

    imagePanel = new JPanel();
    imageLabel = new JLabel(image);
    imagePanel.add(imageLabel);

    // The below part is for JPanel related to the buttons
    buttonPanel = new JPanel(new GridBagLayout());
    GridBagConstraints grid = new GridBagConstraints();
    grid.insets = new Insets(10,10,10,10);
    // Select Buttom
    grid.gridx = 0;
    grid.gridy = 1;
    buttonPanel.add(selectImage,grid);

    // We can determine that how much of the space is going to be belonged to adjustPanel
    buttonPanel.setPreferredSize(new Dimension(110,660));

    setLayout(new BorderLayout());
    add(buttonPanel,BorderLayout.WEST);
    add(imagePanel,BorderLayout.EAST);
    setResizable( false );

}


private ImageIcon selectButtonActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser fc = new JFileChooser(basePath);
    fc.setFileFilter(new ImageFileFilter());
    int res = fc.showOpenDialog(null);
    // We have an image!
    try {
        if (res == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            targetImg = rescale(ImageIO.read(file));
            return new ImageIcon(targetImg);


        } // Oops!
        else {
            JOptionPane.showMessageDialog(null,
                    "You must select one image to be the reference.", "Aborting...",
                    JOptionPane.WARNING_MESSAGE);
        }
    } catch (Exception iOException) {
    }
    return new ImageIcon(targetImg);

}
public BufferedImage rescale(BufferedImage originalImage)
{
    BufferedImage resizedImage = new BufferedImage(baseSizeX, baseSizeY, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, baseSizeX, baseSizeY, null);
    g.dispose();
    return resizedImage;
}

    public static void main(String[] args) throws AWTException{
    JFrame gui = new selectWindow();
    gui.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:0)

您可以根据选择矩形创建新图像。

有关执行此操作的简单方法,请参阅Screen Image