► zoom an image in java

Technology

header ads

► zoom an image in java

zoom a java image from the pc and save it to the desktop


when we have a project in which we must take an image of the pc from java, and with the same OS of java to be able to zoom it for that we are going to take the bookstores AffineTransform,BufferedImage,Graphics2D,ImageIO , what we are going to do is load the image in a buffer then we keep it in a Graphics,we process it with ImageIO and we save it again with another name first the code which is a function to call it the function we will call it
cortaunpedazotyloacerca:






  public void cortaunpedazotyloacerca() throws IOException{
      BufferedImage bufferedImage = ImageIO.read(new File("a.jpg"));
    BufferedImage dstinoimagencambiadabuffer = new BufferedImage(700, 700, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dstinoimagencambiadabuffer.createGraphics();
    AffineTransform httpswwwmbajavacom = AffineTransform.getScaleInstance(2, 2);
    g.drawRenderedImage(bufferedImage, httpswwwmbajavacom);
    ImageIO.write(dstinoimagencambiadabuffer, "JPG", new File("b.jpg"));
    
    }

which is obvious that the image we want to edit is a.jpg and the image we want to zoom it to is b.jpg

for example a.jpg:

zoom an image in java

and when you apply zoom java and cut it would be as follows:
zoom an image in java 2
as we see we gave it a small, small-scale zoom.
we edit the scale in the part of the code AffineTransform.getScaleInstance(2, 2); where 2.2 is the zoom scale and you play with the zoom scale.

it's very easy, if you need to call the function you could call it from a main suppose that the class is called ImgJavaZoom:






/*
 * https://www.mbajava.com/
 * https://www.mbajava.com/
 * and open the template in the editor.
 */
package ensayo;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
 *
 * @author andres
 */
public class ImgJavaZoom {
    public void cortaunpedazotyloacerca() throws IOException{
      BufferedImage bufferedImage = ImageIO.read(new File("a.jpg"));
    BufferedImage dstinoimagencambiadabuffer = new BufferedImage(700, 700, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dstinoimagencambiadabuffer.createGraphics();
    AffineTransform httpswwwmbajavacom = AffineTransform.getScaleInstance(2, 2);
    g.drawRenderedImage(bufferedImage, httpswwwmbajavacom);
    ImageIO.write(dstinoimagencambiadabuffer, "JPG", new File("b.jpg"));
    
    }
    
     public static void main(String[] args) throws Exception {
  ImgJavaZoom e= new ImgJavaZoom();
  e.cortaunpedazotyloacerca();
  }
}












Post a Comment