ImageUtil: 透明化图片处理方法

需积分: 9 0 下载量 100 浏览量 更新于2024-08-05 收藏 5KB TXT 举报
本资源是一份Java代码示例,名为`ImageUtil.java`,主要功能是将图片的背景转换成透明,以便于在需要进行签章或透明融合的应用场景中使用。代码的核心在于处理图片的透明度,通过以下几个步骤实现: 1. 引入必要的库: - `java.awt`: 提供图形用户界面(GUI)组件和图像处理相关的类,如`Graphics2D`和`Image`。 - `javax.imageio.ImageIO`: Java图像I/O工具包,用于读取和写入各种图像格式。 - `javax.swing.ImageIcon`: 显示图像的Swing组件。 2. 主方法`public static void main(String[] args)`: - 定义一个图片路径`imagePath`,这里使用了`ITextTwo.class.getResource()`获取资源路径,这通常用于从类路径中加载资源文件。 - 调用`transferAlpha2File`方法,传入源图片路径和目标保存路径,输出结果到控制台。 3. `public static boolean transferAlpha2File(String imgSrc, String imgTarget)`方法: - 初始化变量,如`File file`、`InputStream is`和布尔值`result`。 - 使用`FileInputStream`打开源图片文件,将`MultipartFile`转换为流的情况已提及,此处未直接使用。 - 使用`ImageIO.read(is)`读取源图片,并将其转换为`BufferedImage`对象。 - 创建一个新的`BufferedImage`对象`bufferedImage`,宽度和高度与原图相同,类型设置为`BufferedImage.TYPE_4BYTE_ABGR`,这种类型支持Alpha通道,便于后续处理透明度。 - 获取`Graphics2D`对象`g2D`,这是进行图形绘制的接口,可以操作像素。 4. 透明处理逻辑: - 使用`Graphics2D`对象`g2D`,通过Alpha通道对图像进行操作。具体而言,可能涉及到遍历每个像素,检查Alpha通道值(代表透明度),并根据需要调整或设置像素透明度。透明度较低的区域会被认为是需要被其他颜色填充的区域。 - 实现透明度转换后,将修改后的`BufferedImage`写入目标文件。 5. 方法结束时,返回处理结果`boolean result`,表示操作是否成功。 这个`ImageUtil`类提供了一个基础的透明化图片处理方法,适用于需要在Java应用中动态调整图片透明度的场景,比如印章或图片合成等。开发者可以根据实际需求对代码进行扩展或优化,以适应不同的图片处理需求。

代码解释:public static Vector<Mat> findPlateByContours(Mat src, Mat inMat, Vector<Mat> dst, Boolean debug, String tempPath) { // 灰度图 Mat gray = new Mat(); ImageUtil.gray(inMat, gray, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_GRAY, gray); // 高斯模糊 Mat gsMat = new Mat(); ImageUtil.gaussianBlur(gray, gsMat, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_GAUSSIAN, gsMat); // Sobel 运算,得到图像的一阶水平方向导数 Mat sobel = new Mat(); ImageUtil.sobel(gsMat, sobel, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_SOBEL, sobel); // 图像进行二值化 Mat threshold = new Mat(); ImageUtil.threshold(sobel, threshold, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_THRESHOLD, threshold); // 使用闭操作 同时处理一些干扰元素 Mat morphology = threshold.clone(); ImageUtil.morphologyClose(threshold, morphology, debug, tempPath); // 闭操作 Imgcodecs.imwrite(tempPath + Constant.TEMP_CLOSE, morphology); // 边缘腐蚀,边缘膨胀,可以多执行两次 morphology = ImageUtil.erode(morphology, debug, tempPath, 4, 4); Imgcodecs.imwrite(tempPath + Constant.TEMP_ERODE, morphology); morphology = ImageUtil.dilate(morphology, debug, tempPath, 4, 4, true); Imgcodecs.imwrite(tempPath + Constant.TEMP_DILATE, morphology); // 保存结果到目标文件 // 将二值图像,resize到原图的尺寸; 如果使用缩小后的图片提取图块,可能会出现变形,影响后续识别结果 ImageUtil.enlarge(morphology, morphology, src.size(), debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_RESIZE, morphology); // 获取图中所有的轮廓 List<MatOfPoint> contours = ImageUtil.contours(src, morphology, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_CONTOUR, morphology); // 根据轮廓, 筛选出可能是车牌的图块 Vector<Mat> blockMat = ImageUtil.screenBlock(src, contours, false, debug, tempPath); // 找出可能是车牌的图块,存到dst中, 返回结果 hasPlate(blockMat, dst, debug, tempPath + "contour/"); return dst; }

2023-05-25 上传

代码解释:public static String charsSegment(Mat inMat, PlateColor color, Boolean debug, String tempPath) { int charCount = 7; // 车牌字符个数 if (color.equals(PlateColor.GREEN)) { charCount = 8; } // 切换到灰度图 Mat gray = new Mat(); Imgproc.cvtColor(inMat, gray, Imgproc.COLOR_BGR2GRAY); ImageUtil.gaussianBlur(gray, gray, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_PLATE_PREDICT, gray); // 图像进行二值化 Mat threshold = new Mat(); switch (color) { case BLUE: Imgproc.threshold(gray, threshold, 10, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY); break; default: // GREEN YELLOW Imgproc.threshold(gray, threshold, 10, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY_INV); break; } ImageUtil.debugImg(debug, tempPath, "plateThreshold", threshold); // 输出二值图 Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_PLATE_PREDICT, threshold); // 边缘腐蚀 threshold = ImageUtil.erode(threshold, debug, tempPath, 2, 2); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_ERODE, threshold); // 垂直方向投影,错切校正 // 理论上,还可以用于分割字符 Integer px = getShearPx(threshold); ImageUtil.shearCorrection(threshold, threshold, px, debug, tempPath); // 前面已经结果错切校正了,可以按照垂直、水平方向投影进行精确定位 // 垂直投影 + 垂直分割线,分割字符 // 水平投影,去掉上下边框、铆钉干扰 threshold = sepAndClear(threshold, px, charCount, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_SEP_AND_CLEAR, threshold); // 边缘膨胀 // 还原腐蚀操作产生的影响 // 会影响中文字符的精确度 threshold = ImageUtil.dilate(threshold, debug, tempPath, 2, 2, true); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_DILATE, threshold); // 提取外部轮廓 List<MatOfPoint> contours = Lists.newArrayList(); Imgproc.findContours(threshold, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); Vector<Rect> charRect = new Vector<Rect>(); // 字符轮廓集合 Mat dst; dst = inMat.clone(); Imgproc.cvtColor(threshold, dst, Imgproc.COLOR_GRAY2BGR); if (debug) {

2023-05-25 上传