| New file |
| | |
| | | package org.springblade.modules.test4j; |
| | | |
| | | import io.swagger.annotations.Api; |
| | | import lombok.AllArgsConstructor; |
| | | import net.sourceforge.tess4j.ITesseract; |
| | | import net.sourceforge.tess4j.Tesseract; |
| | | import net.sourceforge.tess4j.TesseractException; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.modules.test4j.util.Test4jUtil; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @RestController |
| | | @AllArgsConstructor |
| | | @RequestMapping("/test4j") |
| | | @Api(value = "图片智能识别", tags = "图片智能识别") |
| | | public class Test4jController { |
| | | |
| | | @PostMapping("/read-id") |
| | | public R readText(@RequestParam("file") MultipartFile file) { |
| | | try { |
| | | BufferedImage grayscaleImage = grayscale(ImageIO.read(file.getInputStream())); |
| | | String text = recognizeText(grayscaleImage); |
| | | return R.data(text); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public BufferedImage grayscale(BufferedImage image) { |
| | | int width = image.getWidth(); |
| | | int height = image.getHeight(); |
| | | BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); |
| | | Graphics g = result.getGraphics(); |
| | | g.drawImage(image, 0, 0, null); |
| | | g.dispose(); |
| | | return result; |
| | | } |
| | | |
| | | public String recognizeText(BufferedImage image) { |
| | | ITesseract tesseract = new Tesseract(); |
| | | // 设置Tesseract的路径 |
| | | tesseract.setDatapath("F:\\test4jdata"); |
| | | // 设置为中文简体 |
| | | tesseract.setLanguage("chi_sim"); |
| | | try { |
| | | return tesseract.doOCR(image); |
| | | } catch (TesseractException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | } |