Image 的 getRGB方法

本文详细介绍了Java中Graphics类的getRGB方法,该方法用于获取图像的ARGB数据并将其存储为整形数组。文中解释了参数含义,如alpha通道表示的透明度,以及颜色值可能如何反映设备能力。

第一次自己翻译文章,翻译不到位的地方忘体谅!废话少说直接上东西了

函数原型

public void getRGB(int[]rgbData,  intoffset, intscanlength, intx,inty,  intwidth, intheight)
获得一个指定图像区域的ARGB数据并且存储在一个提供的整形数组里,每一个像素的值存储成一个16进制的格式,最高位包含了alpha 通道,
并且也分别保存了图像的R , G , B 值。alpha 通道指明了像素的透明度的问题,其中0x00代表的是全透明,0XFF代表的是不透明!
 返回的值可以不是来至同一个原始的资源,比如说来至createRGBImage或者来至一个PNG图 片 。 颜色的值可能会重复采样来反映这个设备的能力。
哪些不支持alpha通道的设备可能会才用半透明的。
 rgbData :指的是保存这个图片数据的数组
offset : 偏移量,从哪里开始存值
scanlength :在数组中存放的长度
x:图片的左上角坐标(取值处)
y:图片的左上角坐标(取值处)
width:取出的图片的宽度
height:取出的图片的宽度
这里可以帮助理解一下


    rgbData[offset + (a - x) + (b - y) * scanlength] = P(a, b);

@PostConstruct public void initOcrEngine() { tesseract = new Tesseract(); //语言包路径和支持语言 tesseract.setDatapath(tessDataPath); tesseract.setLanguage("eng+chi_sim"); tesseract.setPageSegMode(6); //自动页面分割 tesseract.setOcrEngineMode(3); //LSTM引擎 } /** * OCR识别图片内容 */ private String extractImageText(MultipartFile file) { try (InputStream is = file.getInputStream()) { BufferedImage image = ImageIO.read(is); if (image == null) { return MessageUtils.message("Image.parsing.failed"); } image = increaseVerticalSpacingBetweenText(image, 20, 128); image = increaseHorizontalSpacing(image, 20); saveDebugImage(image, "3_final.png"); // 保存最终处理结果 String result = tesseract.doOCR(image); //OCR识别 result = postProcess(result); result = result.replaceAll("\\s+", " ").trim(); System.out.println("图片内容:\n" + result); return result; } catch (Exception e) { e.printStackTrace(); return MessageUtils.message("file.read.picture.error"); } } // 添加图片保存方法 private void saveDebugImage(BufferedImage img, String filename) throws IOException { // 保存到项目目录下的debug文件夹 String basePath = "D:/pdf/debug/"; File outputDir = new File(basePath); if (!outputDir.exists()) outputDir.mkdirs(); String fullPath = basePath + filename; ImageIO.write(img, "png", new File(fullPath)); } public static BufferedImage increaseVerticalSpacingBetweenText(BufferedImage image, int spacing, int threshold) { int width = image.getWidth(); int height = image.getHeight(); // 二值化处理 BufferedImage binaryImage = binarizeImage(image, threshold); // 检测文字区域 List<int[]> textRegions = detectTextRegions(binaryImage); // 打印检测到的文字区域,用于调试 System.out.println("Detected text regions: " + textRegions); // 计算新的高度 int newHeight = height + (textRegions.size() - 1) * spacing; BufferedImage newImage = new BufferedImage(width, newHeight, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g2d = newImage.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, width, newHeight); int currentY = 0; for (int[] region : textRegions) { int startY = region[0]; int endY = region[1]; // 复制文字区域 for (int y = startY; y < endY; y++) { for (int x = 0; x < width; x++) { int argb = image.getRGB(x, y); newImage.setRGB(x, currentY, argb); } currentY++; } // 添加空隙 currentY += spacing; } g2d.dispose(); return newImage; } // 二值化处理 private static BufferedImage binarizeImage(BufferedImage image, int threshold) { int width = image.getWidth(); int height = image.getHeight(); BufferedImage binaryImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int argb = image.getRGB(x, y); int gray = (argb >> 16) & 0xff; if (gray < threshold) { binaryImage.setRGB(x, y, Color.BLACK.getRGB()); } else { binaryImage.setRGB(x, y, Color.WHITE.getRGB()); } } } return binaryImage; } // 检测文字区域 private static List<int[]> detectTextRegions(BufferedImage binaryImage) { int width = binaryImage.getWidth(); int height = binaryImage.getHeight(); List<int[]> textRegions = new ArrayList<>(); boolean inText = false; int startY = 0; for (int y = 0; y < height; y++) { boolean hasBlackPixel = false; for (int x = 0; x < width; x++) { if (binaryImage.getRGB(x, y) == Color.BLACK.getRGB()) { hasBlackPixel = true; break; } } if (hasBlackPixel && !inText) { startY = y; inText = true; } else if (!hasBlackPixel && inText) { textRegions.add(new int[]{startY, y}); inText = false; } } if (inText) { textRegions.add(new int[]{startY, height}); } return textRegions; } // 水平空隙添加(修复版) public static BufferedImage increaseHorizontalSpacing(BufferedImage image, int spacing) { BufferedImage newImage = new BufferedImage( image.getWidth() + spacing * 2, image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); Graphics2D g2d = newImage.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, newImage.getWidth(), newImage.getHeight()); // 完整绘制(关键参数修正) g2d.drawImage(image,0, spacing , image.getWidth(), image.getHeight(), null); g2d.dispose(); System.out.println("水平处理后图像类型: " + newImage.getType()); System.out.println("水平处理后图像尺寸: " + newImage.getWidth() + "x" + newImage.getHeight()); return newImage; }这个是我的内容 但是我们图片里面的内容 没有弄出空隙出来 比如说我图片里面的扩号紧挨到了8上面 是上下内容 所以导致识别错误的 我这个ocr图片读取文件内容那个图片里面的内容和其他的元素紧挨着就会识别错误
最新发布
07-25
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值