I am trying to read an image, zoom it in to 80*60 and then make it duplicated in size by replication method. My methods work well individually, but when when I call them in the main method my image turns black. Can anyone help me please? Here is my code :
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;
}
}
What I'm proposing is this:
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;
}
}
So, the "core" problem is here...
BufferedImage zoomin = new BufferedImage(
ScaledImage(Image,80,60).getWidth(null),
ScaledImage(Image,80,60).getWidth(null),
BufferedImage.TYPE_BYTE_GRAY);
All this is doing is creating a blank image which is 80x60
and doing a lot of work to achieve it.
Instead, you should be just using something like...
BufferedImage zoomin = ScaledImage(Image,80,60);
Now, if you want to make it grayscale, then you'd need to make a new BufferedImage
image of type BufferedImage.TYPE_BYTE_GRAY
of the same size as the zoomin
image and paint the zoomin
image to it...
You might also like to take a look at Quality of Image after resize very low -- Java and Java: maintaining aspect ratio of JPanel background image for some more ideas about scaling the image. get/setRGB
isn't particularly efficient and the quality of drawImage(x, y, w, h)
isn't that great either
So, after making the suggest modifications, I run your algorithms and it came up with this...
Original, 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;
}
}
so it seems to work okay for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.