| | |
| | | fos.write(pictureData); |
| | | } |
| | | // 图片预处理 |
| | | File processedFile = preprocessImage(tempFile, pictureType); |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | |
| | | // File processedFile = preprocessImage(tempFile, pictureType); |
| | | // 调用 readPngFile1 方法读取图片文字信息 |
| | | String ocrResult = ""; |
| | | try { |
| | | ocrResult = (String) readPngFile1(tempFile); |
| | | // ocrResult = (String) readPngFile1(processedFile); |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | // ocrResult = (String) readPngFile(processedFile); |
| | | } catch (TesseractException e) { |
| | | ocrResult = "OCR识别失败: " + e.getMessage(); |
| | | } finally { |
| | | // 删除临时文件 |
| | | tempFile.delete(); |
| | | processedFile.delete(); |
| | | // processedFile.delete(); |
| | | } |
| | | |
| | | // 将图片信息添加到结果中 |
| | |
| | | ocrText = ocrText.replaceAll("电\\s*压\\s*\\(HV\\)", "电压(KV)"); |
| | | ocrText = ocrText.replaceAll("电\\s*流\\s*\\(nt\\)", "电流(mA)"); |
| | | return ocrText; |
| | | } |
| | | |
| | | /** |
| | | * 对图片进行预处理,包括灰度化、二值化和锐化 |
| | | * @param inputFile 输入的图片文件 |
| | | * @param formatName 图片格式名称 |
| | | * @return 处理后的图片文件 |
| | | * @throws IOException 读取或写入图片时可能抛出的异常 |
| | | */ |
| | | private static File preprocessImage(File inputFile, String formatName) throws IOException { |
| | | // 读取图片 |
| | | BufferedImage image = ImageIO.read(inputFile); |
| | | |
| | | // 灰度化 |
| | | image = convertToGrayscale(image); |
| | | // 二值化 |
| | | image = applyThreshold(image, 128); |
| | | // 锐化 |
| | | image = applySharpening(image); |
| | | |
| | | // 创建处理后的临时文件 |
| | | File outputFile = File.createTempFile(UUID.randomUUID().toString(), "." + formatName); |
| | | ImageIO.write(image, formatName, outputFile); |
| | | return outputFile; |
| | | } |
| | | |
| | | /** |
| | | * 将图片转换为灰度图 |
| | | * @param image 输入的图片 |
| | | * @return 灰度化后的图片 |
| | | */ |
| | | private static BufferedImage convertToGrayscale(BufferedImage image) { |
| | | BufferedImage grayImage = new BufferedImage( |
| | | image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); |
| | | grayImage.getGraphics().drawImage(image, 0, 0, null); |
| | | return grayImage; |
| | | } |
| | | |
| | | /** |
| | | * 对图片进行二值化处理 |
| | | * @param image 输入的图片 |
| | | * @param threshold 二值化阈值 |
| | | * @return 二值化后的图片 |
| | | */ |
| | | private static BufferedImage applyThreshold(BufferedImage image, int threshold) { |
| | | BufferedImage binaryImage = new BufferedImage( |
| | | image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); |
| | | for (int y = 0; y < image.getHeight(); y++) { |
| | | for (int x = 0; x < image.getWidth(); x++) { |
| | | int rgb = image.getRGB(x, y); |
| | | int gray = (rgb >> 16) & 0xff; |
| | | if (gray < threshold) { |
| | | binaryImage.setRGB(x, y, Color.BLACK.getRGB()); |
| | | } else { |
| | | binaryImage.setRGB(x, y, Color.WHITE.getRGB()); |
| | | } |
| | | } |
| | | } |
| | | return binaryImage; |
| | | } |
| | | |
| | | /** |
| | | * 对图片进行锐化处理 |
| | | * @param image 输入的图片 |
| | | * @return 锐化后的图片 |
| | | */ |
| | | private static BufferedImage applySharpening(BufferedImage image) { |
| | | float[] sharpenMatrix = { |
| | | 0f, -1f, 0f, |
| | | -1f, 5f, -1f, |
| | | 0f, -1f, 0f |
| | | }; |
| | | java.awt.image.Kernel kernel = new java.awt.image.Kernel(3, 3, sharpenMatrix); |
| | | java.awt.image.ConvolveOp op = new java.awt.image.ConvolveOp(kernel, java.awt.image.ConvolveOp.EDGE_NO_OP, null); |
| | | return op.filter(image, null); |
| | | } |
| | | |
| | | public static Object readPngFile1(File file) throws IOException, TesseractException { |
| | |
| | | } else { |
| | | path = canonicalPath64.replaceAll("/chi_sim.traineddata", "").replaceAll("\\\\", "/"); |
| | | } |
| | | // 设置 TESSDATA_PREFIX 环境变量 |
| | | // System.setProperty("TESSDATA_PREFIX", path); |
| | | //设置配置文件夹微视、识别语言、识别模式 |
| | | Tesseract tesseract = new Tesseract(); |
| | | tesseract.setDatapath(path); |