【问题标题】:Select an image and then select a sub-Image out of the selected image [duplicate]选择一个图像,然后从所选图像中选择一个子图像[重复]
【发布时间】:2014-07-01 17:59:22
【问题描述】:

在我的 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);
}

}

【问题讨论】:

  • 我认为你需要重构你的代码。把子图处理部分移出构造函数,问题就迎刃而解了
  • “因为,我需要它的信息来从中获取子图像。”什么“信息”?
  • 我的信息是什么意思:如何访问所选图像以应用矩形区域捕获?现在,我还没有应用任何矩形区域捕获,因为我无权访问我上面的代码选择的缓冲图像。

标签: java swing awt jfilechooser


【解决方案1】:

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

请参阅Screen Image 了解执行此操作的简单方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多