All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.wanmo.ocr.service.OcrService Maven / Gradle / Ivy

package cn.net.wanmo.ocr.service;

import cn.net.wanmo.ocr.util.TesseractUtil;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Service
public class OcrService {

    /**
     * 识别图片中的文字
     * @param imageFile 图片文件
     * @return 文字信息
     */
    public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException {
        InputStream is = new ByteArrayInputStream(imageFile.getBytes());
        BufferedImage bufferedImage = ImageIO.read(is);

        Tesseract tesseract = TesseractUtil.getTesseract();
        return tesseract.doOCR(bufferedImage);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy