java 代码未以正确大小导出缓冲图像
java code not exporting buffered Image in correct size
我下面的 java 代码的目标是以 400 X 400 格式导出图像。现在代码确实导出了图像。但是它以原始大小导出图像。我不知道为什么它不以所需的格式导出。没有错误存在。目标只是一些如何获取行
BufferedImage scaledButtonImage = new BufferedImage(400, 400, bimg.getType());
以自己的方式工作。
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ren {
Frame f;
JLabel b2 = new JLabel("");
ren() throws IOException {
f = new JFrame();
b2.setIcon(new ImageIcon("/Users/johnzalubski/Desktop/dropIn/Complete-Audi-Buying-Guide-gear-patrol-lead-full.jpg"));
JButton b3 = new JButton("Exported");
File file = new File("aa.png");
f.add(b2, BorderLayout.CENTER);
f.add(b3, BorderLayout.SOUTH);
f.setSize(400, 500);
f.setVisible(true);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Icon ico = b2.getIcon();
// Create a buffered image
BufferedImage bimg = new BufferedImage(ico.getIconWidth(), ico.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
// Create the graphics context
Graphics g = bimg.createGraphics();
// Now paint the icon
ico.paintIcon(null, g, 0, 0);
g.dispose();
BufferedImage scaledButtonImage =
new BufferedImage(400, 400, bimg.getType());
Graphics g1 = scaledButtonImage.createGraphics();
g1.drawImage(scaledButtonImage, 0, 0, 400, 400, null);
//
g1.dispose();
b2.setIcon(new ImageIcon(bimg));
try {
ImageIO.write(bimg, "png", file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) throws IOException {
new ren();
}
}
如果您想在 scaledButtonImage
中创建 bimg
的缩放版本,则此行:
g1.drawImage(scaledButtonImage, 0, 0, 400, 400, null);
应该是:
g1.drawImage(bimg, 0, 0, scaledButtonImage.getWidth(), scaledButtonImage.getHeight(),
0, 0, bimg.getWidth(), bimg.getHeight() null);
目前您的代码正在将 scaledButtonImage
绘制到自身中。另外,如果你想获得 bimg
的缩放副本,你必须使用 drawImage
的版本,它允许你指定目标和源矩形。
最后,你需要写出scaledButtonImage
,而不是bimg
。更改此行
ImageIO.write(bimg, "png",file);
到
ImageIO.write(scaledButtonImage, "png",file);
我下面的 java 代码的目标是以 400 X 400 格式导出图像。现在代码确实导出了图像。但是它以原始大小导出图像。我不知道为什么它不以所需的格式导出。没有错误存在。目标只是一些如何获取行
BufferedImage scaledButtonImage = new BufferedImage(400, 400, bimg.getType());
以自己的方式工作。
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ren {
Frame f;
JLabel b2 = new JLabel("");
ren() throws IOException {
f = new JFrame();
b2.setIcon(new ImageIcon("/Users/johnzalubski/Desktop/dropIn/Complete-Audi-Buying-Guide-gear-patrol-lead-full.jpg"));
JButton b3 = new JButton("Exported");
File file = new File("aa.png");
f.add(b2, BorderLayout.CENTER);
f.add(b3, BorderLayout.SOUTH);
f.setSize(400, 500);
f.setVisible(true);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Icon ico = b2.getIcon();
// Create a buffered image
BufferedImage bimg = new BufferedImage(ico.getIconWidth(), ico.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
// Create the graphics context
Graphics g = bimg.createGraphics();
// Now paint the icon
ico.paintIcon(null, g, 0, 0);
g.dispose();
BufferedImage scaledButtonImage =
new BufferedImage(400, 400, bimg.getType());
Graphics g1 = scaledButtonImage.createGraphics();
g1.drawImage(scaledButtonImage, 0, 0, 400, 400, null);
//
g1.dispose();
b2.setIcon(new ImageIcon(bimg));
try {
ImageIO.write(bimg, "png", file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) throws IOException {
new ren();
}
}
如果您想在 scaledButtonImage
中创建 bimg
的缩放版本,则此行:
g1.drawImage(scaledButtonImage, 0, 0, 400, 400, null);
应该是:
g1.drawImage(bimg, 0, 0, scaledButtonImage.getWidth(), scaledButtonImage.getHeight(),
0, 0, bimg.getWidth(), bimg.getHeight() null);
目前您的代码正在将 scaledButtonImage
绘制到自身中。另外,如果你想获得 bimg
的缩放副本,你必须使用 drawImage
的版本,它允许你指定目标和源矩形。
最后,你需要写出scaledButtonImage
,而不是bimg
。更改此行
ImageIO.write(bimg, "png",file);
到
ImageIO.write(scaledButtonImage, "png",file);