Home  >  Article  >  Java  >  How to create an image using Java OpenCV library?

How to create an image using Java OpenCV library?

王林
王林forward
2023-09-17 23:53:011001browse

Create an image

  • Use the ImageIO.read() method to read the required image.

  • Get the height and width of the image.

  • Create an empty buffered image to store the results

  • Use a nested for loop to iterate over each pixel in the image.

  • Iterates the width of the image from right to left.

  • Use the getRGB() method to get the pixel value.

  • Use the setRGB() method to set the pixel value to the resulting image object, By replacing the new width value.

Example

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MirrorImage {
   public static void main(String args[])throws IOException {
      //Reading the image
      File file= new File("D:\Images\tree.jpg");
      BufferedImage img = ImageIO.read(file);
      //Getting the height and with of the read image.
      int height = img.getHeight();
      int width = img.getWidth();
      //Creating Buffered Image to store the output
      BufferedImage res = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      for(int j = 0; j < height; j++){
         for(int i = 0, w = width - 1; i < width; i++, w--){
            int p = img.getRGB(i, j);
            //set mirror image pixel value - both left and right
            res.setRGB(w, j, p);
         }
      }
      //Saving the modified image
      file = new File("D:\Images\mirror_image.jpg");
      ImageIO.write(res, "jpg", file);
      System.out.println("Done...");
   }
}

Enter

如何使用Java OpenCV库创建镜像?

Output

如何使用Java OpenCV库创建镜像?

The above is the detailed content of How to create an image using Java OpenCV library?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete