gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:22 25 4
gpt4 key购买 nike

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

}

最佳答案

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

参见Screen Image一个简单的方法来做到这一点。

关于java - 选择一个图像,然后从所选图像中选择一个子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com