Quantcast
Channel: StackExchange Replication Questions
Viewing all articles
Browse latest Browse all 17268

Zooming an image in java by Repication Method

$
0
0

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


}

Viewing all articles
Browse latest Browse all 17268

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>