我正在尝试读取图像,将其放大到80 * 60,然后通过复制方法将其复制大小。我的方法可以单独使用,但是当我在主方法中调用它们时,我的图像会变黑。有人可以帮我吗? 这是我的代码:
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedImage Image = null;
File fc = null;
try{
fc = new File("C:\\1.jpg");
Image = ImageIO.read(fc);
BufferedImage zoomin = new BufferedImage(ScaledImage(Image,80,60).getWidth(null),ScaledImage(Image,80,60).getWidth(null), BufferedImage.TYPE_BYTE_GRAY);
JFrame frame = new JFrame("Scaled Resolution");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel lbl = new JLabel();
lbl.setIcon(new ImageIcon(ImgReplication(zoomin,2)));
frame.getContentPane().add(lbl, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
catch (Exception e1){
System.out.println(e1);
}
}
public static BufferedImage ImgReplication(BufferedImage image, int n) {
int w = n * image.getWidth();
int h = n * image.getHeight();
BufferedImage enlargedImage =
new BufferedImage(w, h, image.getType());
for (int y=0; y < h; ++y)
for (int x=0; x < w; ++x)
enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n));
return enlargedImage;
}
public static BufferedImage ScaledImage(Image img, int w , int h){
BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedImage;
}
}
答案 0 :(得分:1)
我建议的是:
BufferedImage Image = null;
File fc = null;
try{
fc = new File("C:\\1.jpg");
Image = ImageIO.read(fc);
BufferedImage sc=ScaledImage(Image,80,60);
JFrame frame = new JFrame("Scaled Resolution");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel lbl = new JLabel();
lbl.setIcon(new ImageIcon(ImgReplication(sc,2)));
frame.getContentPane().add(lbl, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
catch (Exception e1){
System.out.println(e1);
}
}
public static BufferedImage ImgReplication(BufferedImage image, int n) {
int w = n * image.getWidth();
int h = n * image.getHeight();
BufferedImage enlargedImage =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int y=0; y < h; ++y)
for (int x=0; x < w; ++x)
enlargedImage.setRGB(x, y, image.getRGB(x/n, y/n));
return enlargedImage;
}
public static BufferedImage ScaledImage(Image img, int w , int h){
BufferedImage resizedImage = new BufferedImage(w , h , BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImage.createGraphics();
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedImage;
}
}
答案 1 :(得分:1)
所以,“核心”问题就在这里......
BufferedImage zoomin = new BufferedImage(
ScaledImage(Image,80,60).getWidth(null),
ScaledImage(Image,80,60).getWidth(null),
BufferedImage.TYPE_BYTE_GRAY);
所有这一切都是创建一个80x60
的空白图像,并为实现它做了很多工作。
相反,你应该只使用像...这样的东西。
BufferedImage zoomin = ScaledImage(Image,80,60);
现在,如果你想让它成为灰度,那么你需要制作一个与BufferedImage
图像大小相同的BufferedImage.TYPE_BYTE_GRAY
类型的新zoomin
图像并绘制zoomin
图片给它......
您可能还想查看Quality of Image after resize very low -- Java和Java: maintaining aspect ratio of JPanel background image,了解有关缩放图像的更多想法。 get/setRGB
效率不高,drawImage(x, y, w, h)
的质量也不是那么好
因此,在进行建议修改后,我运行了您的算法,并提出了这个......
原创,ScaledImage
,ImgReplication
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();;
}
}
});
}
public class TestPane extends JPanel {
public TestPane() throws IOException {
BufferedImage master = ImageIO.read(new File("..."));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new JLabel(new ImageIcon(master)), gbc);
add(new JLabel(new ImageIcon(ScaledImage(master, 80, 60))), gbc);
add(new JLabel(new ImageIcon(ImgReplication(master, 2))), gbc);
}
}
public static BufferedImage ScaledImage(Image img, int w, int h) {
BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return resizedImage;
}
public static BufferedImage ImgReplication(BufferedImage image, int n) {
int w = n * image.getWidth();
int h = n * image.getHeight();
BufferedImage enlargedImage
= new BufferedImage(w, h, image.getType());
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
enlargedImage.setRGB(x, y, image.getRGB(x / n, y / n));
}
}
return enlargedImage;
}
}
所以它对我来说似乎没问题