| | |
| | | |
| | | 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.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 |
| | |
| | | |
| | | @PostMapping("/read-id") |
| | | public R readText(@RequestParam("file") MultipartFile file) { |
| | | // 在这里添加识别文本的代码,例如Tesseract OCR |
| | | Tesseract tesseract = new Tesseract(); |
| | | 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 { |
| | | String text = tesseract.doOCR(ImageIO.read(file.getInputStream())); |
| | | return R.data(text); |
| | | } catch (Exception e) { |
| | | return R.data(e.getMessage()); |
| | | return tesseract.doOCR(image); |
| | | } catch (TesseractException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | } |