Package cn.net.pap.common.opencv
Class ImageUtil
java.lang.Object
cn.net.pap.common.opencv.ImageUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringgenerateEmptyJpeg() 的返回值 Base64.getEncoder().encodeToString(bytes); Base64.getDecoder().decode(s) response.getOutputStream().write(java.util.Base64.getDecoder().decode(oneXOneJpg)); -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbase64ToImage(String base64String, String outputFilePath) base64ToImagestatic BufferedImagecover(BufferedImage originalImage, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int targetX, int targetY) 从一个区域获得信息覆盖到另一个区域static BufferedImagecreateImageWithRegions(int width, int height, Color backColor, int[][] regions) 创建一个指定宽高的图像,并在指定区域内添加不同的颜色static BooleancropImageCut(String inputFilePath, String outputFilePath, int x, int y, int width, int height) 高效的图像裁剪功能, 指定原始图像路径和裁剪后的图像路径, 指定左上角的x/y 和 宽高 进行高效的裁剪 相比于 ImageIO.read("").getSubimage(x, y, width, height), 在极端情况下能够提示执行效率10倍以上。 实验过程中一张7.35M大小的JPG图像(分辨率 9007 x 6221),当前方法执行时间小于1s, ImageIO.read("").getSubimage 执行时间24s.cropImageCutList(String inputFilePath, List<Rectangle> regions) 在一张图像中同时截取多个矩形区域并按顺序返回 base64.static byte[]创建1x1像素的最小JPEG图像 背景色白色 的 jpg 图像字符串, 可以直接 response.getOutputStream().write(generateEmptyJpeg()); 写入响应流static booleanhorizontalMirror(String inputFilePath, String outputFilePath) 水平镜像static BufferedImagemergeByPointInTwoPic(BufferedImage leftImage, int leftPoint1X, int leftPoint1Y, int leftPoint2X, int leftPoint2Y, BufferedImage rightImage, int rightPoint1X, int rightPoint1Y, int rightPoint2X, int rightPoint2Y) 根据指定坐标合并两张图像static BufferedImagemergeImages(String img1Path, String img2Path, int x1, int y1, int x2, int y2) 将两张图片根据给定的坐标点进行拼接static voidrotate90ClockwiseAndOverwrite(File imageFile) 顺时针旋转图像90度并覆盖原图static booleanrotateImage(String inputFilePath, String outputFilePath, double angle) 图像旋转static booleanscaleAndGray(String inputPath, String outputPath, Integer widthHeight) 图像等比缩放并灰度化static booleanverticalMirror(String inputFilePath, String outputFilePath) 垂直镜像
-
Field Details
-
oneXOneJpg
generateEmptyJpeg() 的返回值 Base64.getEncoder().encodeToString(bytes); Base64.getDecoder().decode(s) response.getOutputStream().write(java.util.Base64.getDecoder().decode(oneXOneJpg));- See Also:
-
-
Constructor Details
-
ImageUtil
public ImageUtil()
-
-
Method Details
-
generateEmptyJpeg
public static byte[] generateEmptyJpeg()创建1x1像素的最小JPEG图像 背景色白色 的 jpg 图像字符串, 可以直接 response.getOutputStream().write(generateEmptyJpeg()); 写入响应流- Returns:
-
cropImageCut
public static Boolean cropImageCut(String inputFilePath, String outputFilePath, int x, int y, int width, int height) 高效的图像裁剪功能, 指定原始图像路径和裁剪后的图像路径, 指定左上角的x/y 和 宽高 进行高效的裁剪 相比于 ImageIO.read("").getSubimage(x, y, width, height), 在极端情况下能够提示执行效率10倍以上。 实验过程中一张7.35M大小的JPG图像(分辨率 9007 x 6221),当前方法执行时间小于1s, ImageIO.read("").getSubimage 执行时间24s.- Parameters:
inputFilePath-outputFilePath-x-y-width-height-- Returns:
-
cropImageCutList
在一张图像中同时截取多个矩形区域并按顺序返回 base64.- Parameters:
inputFilePath-regions-- Returns:
-
scaleAndGray
图像等比缩放并灰度化- Parameters:
inputPath-outputPath-widthHeight-- Returns:
-
mergeImages
public static BufferedImage mergeImages(String img1Path, String img2Path, int x1, int y1, int x2, int y2) throws IOException 将两张图片根据给定的坐标点进行拼接- Parameters:
img1Path- 第一张图片的路径img2Path- 第二张图片的路径x1- 第一张图片放置的x坐标y1- 第一张图片放置的y坐标x2- 第二张图片放置的x坐标y2- 第二张图片放置的y坐标- Returns:
- 拼接后的图片
- Throws:
IOException
-
rotateImage
图像旋转- Parameters:
inputFilePath-outputFilePath-angle-- Returns:
-
horizontalMirror
public static boolean horizontalMirror(String inputFilePath, String outputFilePath) throws Exception 水平镜像- Returns:
- Throws:
Exception
-
verticalMirror
垂直镜像- Parameters:
inputFilePath-outputFilePath-- Returns:
-
cover
public static BufferedImage cover(BufferedImage originalImage, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int targetX, int targetY) 从一个区域获得信息覆盖到另一个区域- Parameters:
originalImage-sourceX-sourceY-sourceWidth-sourceHeight-targetX-targetY-- Returns:
-
mergeByPointInTwoPic
public static BufferedImage mergeByPointInTwoPic(BufferedImage leftImage, int leftPoint1X, int leftPoint1Y, int leftPoint2X, int leftPoint2Y, BufferedImage rightImage, int rightPoint1X, int rightPoint1Y, int rightPoint2X, int rightPoint2Y) 根据指定坐标合并两张图像- Parameters:
leftImage- 左图像leftPoint1X- 左图像上边点 X 坐标leftPoint1Y- 左图像上边点 Y 坐标leftPoint2X- 左图像下边点 X 坐标leftPoint2Y- 左图像下边点 Y 坐标rightImage- 右图像rightPoint1X- 右图像上边点 X 坐标rightPoint1Y- 右图像上边点 Y 坐标rightPoint2X- 右图像下边点 X 坐标rightPoint2Y- 右图像下边点 Y 坐标- Returns:
-
createImageWithRegions
public static BufferedImage createImageWithRegions(int width, int height, Color backColor, int[][] regions) 创建一个指定宽高的图像,并在指定区域内添加不同的颜色- Parameters:
width- 图像的宽度height- 图像的高度regions- 一个二维数组,每个子数组包含四个元素:x, y, width, height(区域的左上角坐标和宽高) 以及第五个元素:颜色值(例如:Color.RED.getRGB())- Returns:
- 创建的BufferedImage对象
-
base64ToImage
base64ToImage- Parameters:
base64String-outputFilePath-- Throws:
IOException
-
rotate90ClockwiseAndOverwrite
顺时针旋转图像90度并覆盖原图- Parameters:
imageFile- 图像文件- Throws:
IOException- 如果处理失败
-