กลุ่มผลิตภัณฑ์คลาวด์ Aspose.OCR

Aspose.OCR Cloud ช่วยให้คุณสามารถทำ Optical Character Recognition และการสแกนเอกสารบน Cloud ได้ รองรับการอ่านและจดจำข้อความจากรูปแบบภาพแรสเตอร์ที่ใช้กันทั่วไป (BMP, JPG, GIF, PNG, TIFF) ทำการจดจำอักขระบนรูปภาพที่มีบรรทัดโค้ดน้อยกว่า เพียงส่งรูปภาพเฉพาะไปยัง Aspose.OCR Cloud API จากนั้นระบบจะตอบกลับด้วยข้อความที่จดจำได้ API นี้สามารถจดจำข้อความภาษาอังกฤษ ฝรั่งเศส สเปน และส่งคืนการตอบกลับในรูปแบบ XML หรือ JSON ในบทความนี้ เราจะพูดถึงขั้นตอนในการทำ OCR บนรูปภาพโดยใช้ Java SDK

ในระหว่างกระบวนการจดจำ คุณสามารถอ่านอักขระและข้อมูลแบบอักษรที่เกี่ยวข้องได้ คุณสามารถทำ OCR กับภาพทั้งหมดหรือระบุพิกัด X และ Y เพื่อดำเนินการกับส่วนเฉพาะของภาพแรสเตอร์ นอกจากนี้ ยังสามารถทำการแก้ไขความเอียงอัตโนมัติ รวมถึงการตรวจจับเค้าโครงเอกสารโดยอัตโนมัติและด้วยตนเองด้วยความเร็วสูง เนื่องจากไม่ต้องพึ่งพาทรัพยากรฮาร์ดแวร์

แพลตฟอร์มอิสระ

Cloud API นั้นเป็นอิสระจากระบบปฏิบัติการ ระบบฐานข้อมูล หรือภาษาการพัฒนาของคุณโดยสิ้นเชิง และคุณสามารถใช้ภาษาและแพลตฟอร์มใดก็ได้ที่รองรับ HTTP เพื่อโต้ตอบกับ API ของเรา อย่างไรก็ตาม การเขียนโค้ดไคลเอนต์ด้วยตนเองอาจเป็นเรื่องยาก มีโอกาสเกิดข้อผิดพลาด และใช้เวลานาน ดังนั้น เพื่อให้ลูกค้าของเราสามารถใช้ภาษา Java ได้ จึงได้มีการเผยแพร่ Aspose.OCR Cloud Java SDK เฉพาะภาษา เมื่อใช้ SDK SDK จะดูแลรายละเอียดระดับต่ำจำนวนมากในขณะที่ส่งคำขอและจัดการการตอบกลับ และทำให้คุณสามารถมุ่งเน้นไปที่การเขียนโค้ดที่เฉพาะเจาะจงกับความต้องการของโครงการของคุณโดยเฉพาะได้

จดจำข้อความจากภาพ URL

API ของ Cloud OCR มอบตัวเลือกในการดำเนินการจดจำข้อความบนไฟล์รูปภาพที่พร้อมใช้งานผ่าน URL บนเว็บโดยตรง คุณไม่จำเป็นต้องอัปโหลดไปยังที่เก็บข้อมูลบนคลาวด์โดยเฉพาะ

ตัวอย่างโค้ด Java

private static OcrApi api;
private static final String url = "https://upload.wikimedia.org/wikipedia/commons/2/2f/Book_of_Abraham_FirstPage.png";

public static void main(String args[]) throws IOException {
  try {
	  		setUpConfig();
  } catch (Exception e) {
    // TODO สร้างบล็อค catch อัตโนมัติ
    e.printStackTrace();
  }
  String text;

  text = recognizeByURL();
  System.out.println(text);
}

// วิธีการในการรับรู้ข้อความจากรูปภาพที่โฮสต์บน URL
private static String recognizeByURL() {
  try {
    api = new ApiClient().createService(OcrApi.class);
    Call<ResponseBody> call = api.RecognizeFromUrl(url);
    Response<ResponseBody> res = call.execute();
    ResponseBody answer = res.body();

    com.aspose.ocr.OCRResponse ocrResponse = com.aspose.ocr.OCRResponse.Deserialize(answer);
    String text = ocrResponse.text;

    return text;
    } catch (IOException e) {
      e.printStackTrace();
      return "";
    }
}

private static void setUpConfig() throws Exception {
  Configuration.setAPP_SID("xxxxx");
  Configuration.setAPI_KEY("xxxxx");

  Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
  Configuration.setBasePath("https://api.aspose.cloud/v3.0");
  Configuration.setUserAgent("WebKit");
  Configuration.setTestSrcDir("sourceTest");
  Configuration.setTestDstDir("destTest");

  if (Configuration.getAPI_KEY().isEmpty() || Configuration.getAPP_SID().isEmpty()) {
      System.out.println("! Error: Setup AppSID & AppKey in BaseTest Configuration");
      throw new Exception("Setup AppSID & AppKey in BaseTest Configuration");
    }
}

จดจำข้อความจากภาพในที่เก็บข้อมูล

ตัวอย่างโค้ด Java

private static OcrApi api;

public static void main(String args[]) throws IOException {
  try {
	  		setUpConfig();
  } catch (Exception e) {
    // TODO สร้างบล็อค catch อัตโนมัติ
    e.printStackTrace();
  }
  String text;

  text = recognizeByContent();
  System.out.println(text);
}

private static String recognizeByContent() {
  try {
      File f = new File(Configuration.getTestSrcDir(), "0.png");
      if (!f.exists()) {
      return "Error: recognizeByContentLang: file not found";
      }

      api = new ApiClient().createService(OcrApi.class);
      RequestBody requestBody = RequestBody.create(f,MediaType.parse("application/octet-stream"));
      Call<ResponseBody> call = api.RecognizeFromContent(requestBody);
      Response<ResponseBody> res = call.execute();

      ResponseBody answer = res.body();
      com.aspose.ocr.OCRResponse ocrResponse = com.aspose.ocr.OCRResponse.Deserialize(answer);
      String text = ocrResponse.text;

      return text;

  } catch (IOException e) {
      e.printStackTrace();
      return "";
  }
}

private static void setUpConfig() throws Exception {
  Configuration.setAPP_SID("xxxxx");
  Configuration.setAPI_KEY("xxxxx");

  Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
  Configuration.setBasePath("https://api.aspose.cloud/v3.0");
  Configuration.setUserAgent("WebKit");
  Configuration.setTestSrcDir("sourceTest");
  Configuration.setTestDstDir("destTest");

  if (Configuration.getAPI_KEY().isEmpty() || Configuration.getAPP_SID().isEmpty()) {
      System.out.println("! Error: Setup AppSID & AppKey in BaseTest Configuration");
      throw new Exception("Setup AppSID & AppKey in BaseTest Configuration");
    }
}

คำสั่ง cURL

นอกจากนี้ยังสามารถเข้าถึง Cloud API ได้โดยใช้คำสั่ง cURL และ Aspose.OCR Cloud ได้เช่นกันโดยใช้คำสั่ง cURL อย่างไรก็ตาม โปรดทราบว่าเพื่อเข้าถึง API เราจำเป็นต้องสร้างโทเค็นการเข้าถึง JWT ตามข้อมูลประจำตัวไคลเอนต์ส่วนบุคคลของเรา โปรดดำเนินการคำสั่งต่อไปนี้เพื่อสร้างโทเค็นการเข้าถึง JWT

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=4ccf1790-accc-41e9-8d18-a78dbb2ed1aa&client_secret=caac6e3d4a4724b2feb53f4e460eade3" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

เมื่อสร้างโทเค็น JWT แล้ว โปรดใช้คำสั่งต่อไปนี้เพื่อดำเนินการ OCR บนรูปภาพ

curl "https://api.aspose.cloud/v3.0/ocr/MyImage.png/recognize?language=1" \
-X GET \
-H  "accept: application/json" \
-H  "authorization: Bearer <jwt token>"

ขอ URL

https://api.aspose.cloud/v3.0/ocr/MyImage.png/recognize?language=1

เนื้อหาตอบกลับ

{
  "text": "MOORE STEPHENS",
  "code": 200
}

บทสรุป

ในบทความนี้ เราได้กล่าวถึงรายละเอียดเกี่ยวกับวิธีการทำ OCR บนรูปภาพ นอกจากนี้ เราได้สำรวจตัวเลือกในการดำเนินการจดจำอักขระด้วยแสงบนรูปภาพโดยใช้คำสั่ง cURL โปรดทราบว่าซอร์สโค้ดทั้งหมดของ SDK นั้นมีอยู่ใน GitHub ในทำนองเดียวกัน เราขอแนะนำให้เรียนรู้เพิ่มเติมเกี่ยวกับ API โดยสำรวจผลิตภัณฑ์ Documentation

บทความที่เกี่ยวข้อง