SR.BARMAKI
SR.BARMAKI

Reputation: 11

How to duplicate height of an image file in java

I have an image and trying to create the same image under it(duplicate it). it means that if i have 100*100 size photo i want to change it's size to 100*200 then i write it in an other image file. here is the problem that i don't know how to make the output file's height exactly the size i want to write. please help me handling this situation! thank you. my code is here that i tried for duplication :

public void duplicateImage() throws IOException {


for (int i = 0; i < width; i++) {
   for (int j = 0; j < height; j++) {
    int pixel = image.getRGB(i, j);
    int alpha = (pixel & 0x0000ff00) >> 24;
    int red = (pixel & 0x00ff0000) >> 16;
    int green = (pixel & 0x0000ff00) >> 8;
    int blue = (pixel & 0x000000ff);
    int mid = (red + green + blue) / 3;
    red = blue = green = mid;
    int newPixel = alpha;
    newPixel = (newPixel << 8) + red;
    newPixel = (newPixel << 8) + green;
    newPixel = (newPixel << 8) + blue;
    image.setRGB(i, j, newPixel);
    image.setRGB(i+width,j+height,newPixel);
   }
   }
 }

Upvotes: 0

Views: 43

Answers (1)

SR.BARMAKI
SR.BARMAKI

Reputation: 11

I found a way using BufferedImage to change the photo size. It was simple and I share it so others can understand it too. The https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance(int,%20int,%20int) is a good way to handle by the way. Here's my answer :

    public void duplicateImage(File inFile,File outFile) throws IOException {
      BufferedImage img = new BufferedImage(width,2*height,BufferedImage.TYPE_3BYTE_BGR);
      BufferedImage image = ImageIO.read(inFile)
      for (int i = 0; i < width; i++) {
       for (int j = 0; j < 2*height; j++) {
        int pixel = image.getRGB(i, j%height);
        img.setRGB(i, j, pixel);
       }
       }
       ImageIO.write(img,"jpg",outFile);
     }

Upvotes: 1

Related Questions