java">@Tag(name = "pdf/word/图片文字识别")
word">public word">class OcrController word">extends BaseController {@Autowiredword">private OcrService ocrService;@Autowiredword">private BaiduOcrServiceImpl baiduOcrService;@PostMapping("/recognize-text")@Operation(summary = "pdf/word识别文字", description = "识别")word">public String recognizeText(@RequestParam("file") MultipartFile file) {word">return ocrService.recognizeText(file);}}
java">word">package com.jt.console.service.impl;word">import com.jt.common.beans.ServiceAssert;
word">import com.jt.console.service.OcrService;
word">import org.apache.pdfbox.cos.COSName;
word">import org.apache.pdfbox.pdmodel.PDDocument;
word">import org.apache.pdfbox.pdmodel.PDPage;
word">import org.apache.pdfbox.pdmodel.PDPageTree;
word">import org.apache.pdfbox.pdmodel.PDResources;
word">import org.apache.pdfbox.pdmodel.graphics.PDXObject;
word">import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
word">import org.apache.pdfbox.text.PDFTextStripper;
word">import org.apache.poi.hwpf.HWPFDocument;
word">import org.apache.poi.hwpf.extractor.WordExtractor;
word">import org.apache.poi.openxml4j.util.ZipSecureFile;
word">import org.apache.poi.xwpf.usermodel.*;
word">import org.springframework.beans.factory.annotation.Autowired;
word">import org.springframework.stereotype.Service;
word">import org.springframework.web.multipart.MultipartFile;word">import javax.imageio.ImageIO;
word">import java.awt.image.BufferedImage;
word">import java.io.File;
word">import java.io.FileOutputStream;
word">import java.io.IOException;
word">import java.io.InputStream;
word">import java.net.URLEncoder;
word">import java.util.Base64;word">import word">static com.jt.console.service.impl.BaiduOcrServiceImpl.formatOcrResult;
@Service
word">public word">class OcrServiceImpl word">implements OcrService {@Autowiredword">private BaiduOcrServiceImpl baiduOcrService;@Overrideword">public String recognizeText(MultipartFile file) {String contentType = file.getContentType();word">if (contentType == word">null) {ServiceAssert.isTrue(false, "文件类型不支持");word">return word">null;}InputStream inputStream = word">null;word">try {inputStream = file.getInputStream();word">if (contentType.equals("application/pdf")) {word">return extractTextFromPdf(inputStream);} word">else word">if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ||contentType.equals("application/x-tika-ooxml")) {word">return extractTextFromDocx(inputStream);} word">else word">if (contentType.equals("application/msword")) {word">return extractTextFromDoc(inputStream);} word">else {ServiceAssert.isTrue(false, "不支持的文件类型");word">return word">null;}} word">catch (Exception e) {e.printStackTrace();ServiceAssert.isTrue(false, "处理文件出错");word">return word">null;} word">finally {word">if (inputStream != word">null) {word">try {inputStream.close();} word">catch (IOException e) {e.printStackTrace();}}}}word">private String extractTextFromPdf(InputStream inputStream) word">throws IOException {StringBuilder text = word">new StringBuilder();word">try (PDDocument document = PDDocument.load(inputStream)) {System.setProperty("org.apache.pdfbox.logging.SILENT", "true");PDFTextStripper pdfStripper = word">new PDFTextStripper();text.append(pdfStripper.getText(document));}word">return text.toString();}word">private String extractTextFromDocx(InputStream inputStream) word">throws IOException {StringBuilder text = word">new StringBuilder();ZipSecureFile.setMinInflateRatio(0.001); word">try (XWPFDocument document = word">new XWPFDocument(inputStream)) {document.getParagraphs().forEach(paragraph -> text.append(paragraph.getText()).append("\n"));word">for (XWPFTable table : document.getTables()) {word">for (XWPFTableRow row : table.getRows()) {word">for (XWPFTableCell cell : row.getTableCells()) {text.append(cell.getText()).append("\t");}text.append("\n");}}}word">return text.toString();}word">private String extractTextFromDoc(InputStream inputStream) word">throws IOException {StringBuilder text = word">new StringBuilder();word">try (HWPFDocument document = word">new HWPFDocument(inputStream)) {WordExtractor extractor = word">new WordExtractor(document);String[] paragraphs = extractor.getParagraphText();word">for (String paragraph : paragraphs) {text.append(paragraph).append("\n");}}word">return text.toString();}word">private word">void extractImagesFromPdf(PDDocument document) word">throws IOException {PDPageTree pages = document.getPages();word">int imageCounter = 0;word">for (PDPage page : pages) {PDResources resources = page.getResources();word">for (COSName xObjectName : resources.getXObjectNames()) {PDXObject xObject = resources.getXObject(xObjectName);word">if (xObject word">instanceof PDImageXObject) {PDImageXObject image = (PDImageXObject) xObject;BufferedImage bufferedImage = image.getImage();File imageFile = word">new File("image" + (++imageCounter) + ".png");word">try (FileOutputStream fos = word">new FileOutputStream(imageFile)) {ImageIO.write(bufferedImage, "PNG", fos);}}}}}word">public String extractImagesFromDocx(XWPFDocument document, word">boolean urlEncode) word">throws IOException {StringBuilder recognitionResults = word">new StringBuilder();word">int imageCounter = 0;word">for (XWPFPictureData pictureData : document.getAllPictures()) {word">byte[] bytes = pictureData.getData();String base64Image = Base64.getEncoder().encodeToString(bytes);word">if (urlEncode) {base64Image = URLEncoder.encode(base64Image, "utf-8");}String ocrResult = baiduOcrService.recognizeImage(base64Image);String formattedResult = formatOcrResult(ocrResult);recognitionResults.append("Image ").append(++imageCounter).append(": ").append(formattedResult).append("\n");}word">return recognitionResults.toString();}}
java">word">package com.jt.console.service.impl;word">import com.alibaba.fastjson.JSON;
word">import com.alibaba.fastjson.JSONObject;
word">import com.jt.common.beans.ServiceAssert;
word">import okhttp3.*;
word">import org.springframework.beans.factory.annotation.Value;
word">import org.springframework.stereotype.Service;
word">import org.springframework.web.multipart.MultipartFile;word">import java.io.IOException;
word">import java.net.URLEncoder;
word">import java.util.Base64;
word">import java.util.List;
word">import java.util.Arrays;
@Service("baiduOcrServiceImpl")
word">public word">class BaiduOcrServiceImpl {@Value("${baidu.ocr.apiKey}")word">private String API_KEY; @Value("${baidu.ocr.secretKey}")word">private String SECRET_KEY; word">private word">static word">final List<String> SUPPORTED_FORMATS = Arrays.asList("png", "jpg", "jpeg", "bmp", "gif");word">private word">static word">final OkHttpClient HTTP_CLIENT = word">new OkHttpClient().newBuilder().build();word">private String getAccessToken() word">throws IOException {MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY+ "&client_secret=" + SECRET_KEY);Request request = word">new Request.Builder().url("https://aip.baidubce.com/oauth/2.0/token").method("POST", body).addHeader("Content-Type", "application/x-www-form-urlencoded").build();Response response = HTTP_CLIENT.newCall(request).execute();word">if (!response.isSuccessful()) {String errorMessage = "OCR request failed. Status code: " + response.code() + ", Message: " + response.message();ServiceAssert.isTrue(false, errorMessage);}String responseBody = response.body().string();JSONObject jsonObject = JSON.parseObject(responseBody);word">return jsonObject.getString("access_token");}word">public String recognizeImage(String base64Image) word">throws IOException {MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");RequestBody body = RequestBody.create(mediaType, "image=" + base64Image + "&detect_direction=false¶graph=false&probability=false");Request request = word">new Request.Builder().url("https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + getAccessToken()).method("POST", body).addHeader("Content-Type", "application/x-www-form-urlencoded").addHeader("Accept", "application/json").build();word">try (Response response = HTTP_CLIENT.newCall(request).execute()) {word">if (!response.isSuccessful()) {String errorMessage = "Failed to obtain access token. Status code: " + response.code() + ", Message: " + response.message();ServiceAssert.isTrue(false, errorMessage);}word">return formatOcrResult(response.body().string());}}word">public String convertToBase64(MultipartFile file, word">boolean urlEncode) word">throws IOException {String filename = file.getOriginalFilename();word">if (filename == word">null) {ServiceAssert.isTrue(false, "文件名为空");}String extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();word">if (!SUPPORTED_FORMATS.contains(extension)) {ServiceAssert.isTrue(false, "不支持的图片格式: " + extension);}word">byte[] bytes = file.getBytes();String base64 = Base64.getEncoder().encodeToString(bytes);word">if (urlEncode) {base64 = URLEncoder.encode(base64, "utf-8");}word">return base64;}word">public word">static String formatOcrResult(String ocrResult) {StringBuilder resultText = word">new StringBuilder();word">try {JSONObject jsonObject = JSON.parseObject(ocrResult);word">if (jsonObject.containsKey("words_result")) {word">var wordsResult = jsonObject.getJSONArray("words_result");word">if (wordsResult != word">null && !wordsResult.isEmpty()) {word">for (word">int i = 0; i < wordsResult.size(); i++) {JSONObject wordObject = wordsResult.getJSONObject(i);String word = wordObject.getString("words");word">if (word != word">null && !word.isEmpty()) {resultText.append(word).append(" ");}}} word">else {word">return "";}} word">else {word">return "";}} word">catch (Exception e) {ServiceAssert.isTrue(false,e.getMessage());}word">return resultText.toString().trim();}
}