创建镜像
使用imageio.read()方法读取所需的图像。
获取图像的高度和宽度。
创建一个空的缓冲图像来存储结果
使用嵌套的 for 循环遍历图像中的每个像素。
从右到左迭代图像的宽度。
使用 getrgb() 方法获取像素值。
使用 setrgb() 方法将像素值设置为结果图像对象,通过替换新的宽度值。
示例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..."); }}
输入
输出
以上就是如何使用java opencv库创建镜像?的详细内容。
