gpt4 book ai didi

java - java中通过复制方法缩放图像

转载 作者:行者123 更新时间:2023-12-02 03:41:13 24 4
gpt4 key购买 nike

我正在尝试读取图像,将其放大到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;
}


}

最佳答案

我的建议是这样的:

    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;
}


}

关于java - java中通过复制方法缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813921/

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