Home > Java > javaTutorial > body text

How to create an image using Java OpenCV library?

王林
Release: 2023-09-17 23:53:01
forward
1054 people have browsed it

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...");
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template