General (Template) OCR
    • PDF

    General (Template) OCR

    • PDF

    The content is currently unavailable in Ja - 日本語. You are viewing the default Korean version.
    記事の要約

    Classic/VPC 환경에서 이용 가능합니다.

    CLOVA OCR 서비스의 General/Template OCR API 예제를 소개합니다.

    Content-Type: multipart/form-data인 경우

    요청 헤더 Content-Typemultipart/form-data인 경우의 API 예제를 설명합니다.

    Java

    Java 기반의 API 예제 코드는 다음과 같습니다.

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.UUID;
    
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public class OCRGeneralAPIDemo {
    
    	public static void main(String[] args) {
    		String apiURL = "YOUR_API_URL";
    		String secretKey = "YOUR_SECRET_KEY";
    		String imageFile = "YOUR_IMAGE_FILE";
    
    		try {
    			URL url = new URL(apiURL);
    			HttpURLConnection con = (HttpURLConnection)url.openConnection();
    			con.setUseCaches(false);
    			con.setDoInput(true);
    			con.setDoOutput(true);
    			con.setReadTimeout(30000);
    			con.setRequestMethod("POST");
    			String boundary = "----" + UUID.randomUUID().toString().replaceAll("-", "");
    			con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    			con.setRequestProperty("X-OCR-SECRET", secretKey);
    
    			JSONObject json = new JSONObject();
    			json.put("version", "V2");
    			json.put("requestId", UUID.randomUUID().toString());
    			json.put("timestamp", System.currentTimeMillis());
    			JSONObject image = new JSONObject();
    			image.put("format", "jpg");
    			image.put("name", "demo");
    			JSONArray images = new JSONArray();
    			images.put(image);
    			json.put("images", images);
    			String postParams = json.toString();
    
    			con.connect();
    			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    			long start = System.currentTimeMillis();
    			File file = new File(imageFile);
    			writeMultiPart(wr, postParams, file, boundary);
    			wr.close();
    
    			int responseCode = con.getResponseCode();
    			BufferedReader br;
    			if (responseCode == 200) {
    				br = new BufferedReader(new InputStreamReader(con.getInputStream()));
    			} else {
    				br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
    			}
    			String inputLine;
    			StringBuffer response = new StringBuffer();
    			while ((inputLine = br.readLine()) != null) {
    				response.append(inputLine);
    			}
    			br.close();
    
    			System.out.println(response);
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    
    	private static void writeMultiPart(OutputStream out, String jsonMessage, File file, String boundary) throws
    		IOException {
    		StringBuilder sb = new StringBuilder();
    		sb.append("--").append(boundary).append("\r\n");
    		sb.append("Content-Disposition:form-data; name=\"message\"\r\n\r\n");
    		sb.append(jsonMessage);
    		sb.append("\r\n");
    
    		out.write(sb.toString().getBytes("UTF-8"));
    		out.flush();
    
    		if (file != null && file.isFile()) {
    			out.write(("--" + boundary + "\r\n").getBytes("UTF-8"));
    			StringBuilder fileString = new StringBuilder();
    			fileString
    				.append("Content-Disposition:form-data; name=\"file\"; filename=");
    			fileString.append("\"" + file.getName() + "\"\r\n");
    			fileString.append("Content-Type: application/octet-stream\r\n\r\n");
    			out.write(fileString.toString().getBytes("UTF-8"));
    			out.flush();
    
    			try (FileInputStream fis = new FileInputStream(file)) {
    				byte[] buffer = new byte[8192];
    				int count;
    				while ((count = fis.read(buffer)) != -1) {
    					out.write(buffer, 0, count);
    				}
    				out.write("\r\n".getBytes());
    			}
    
    			out.write(("--" + boundary + "--\r\n").getBytes("UTF-8"));
    		}
    		out.flush();
    	}
    }
    

    Python

    Python 기반의 API 예제 코드는 다음과 같습니다.

    import requests
    import uuid
    import time
    import json
    
    api_url = 'YOUR_API_URL'
    secret_key = 'YOUR_SECRET_KEY'
    image_file = 'YOUR_IMAGE_FILE'
    
    request_json = {
        'images': [
            {
                'format': 'jpg',
                'name': 'demo'
            }
        ],
        'requestId': str(uuid.uuid4()),
        'version': 'V2',
        'timestamp': int(round(time.time() * 1000))
    }
    
    payload = {'message': json.dumps(request_json).encode('UTF-8')}
    files = [
      ('file', open(image_file,'rb'))
    ]
    headers = {
      'X-OCR-SECRET': secret_key
    }
    
    response = requests.request("POST", api_url, headers=headers, data = payload, files = files)
    
    print(response.text.encode('utf8'))
    
    

    PHP

    PHP 기반의 API 예제 코드는 다음과 같습니다.

    <?php
      $client_secret = "YOUR_SECRET_KEY";
      $url = "YOUR_API_URL";
      $image_file = "YOUR_IMAGE_FILE";
    
      $params->version = "V2";
      $params->requestId = uniqid();
      $params->timestamp = time();
      $image->format = "jpg";
      $image->name = "demo";
      $images = array($image);
      $params->images = $images;
      $json = json_encode($params);
      
      $boundary = uniqid();
      $is_post = true;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, $is_post);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $post_form = array("message" => $json, "file" => new CURLFILE($image_file));
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_form);
      $headers = array();
      $headers[] = "X-OCR-SECRET: ".$client_secret;
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      $response = curl_exec($ch);
      $err = curl_error($ch);
      $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close ($ch);
    
      echo $status_code;
      if($status_code == 200) {
        echo $response;
      } else {
        echo "ERROR: ".$response;
      }
    ?>
    

    JavaScript

    JavaScript 기반의 API 예제 코드는 다음과 같습니다.

    const FormData = require('form-data')
    const axios = require('axios')
    
    function requestWithBase64 () {
      axios
        .post(
          '', // APIGW Invoke URL
          {
            images: [
              {
                format: '', // file format
                name: '', // image name
                data: '' // image base64 string(only need part of data). Example: base64String.split(',')[1]
              }
            ],
            requestId: '', // unique string
            timestamp: 0,
            version: 'V2'
          },
          {
            headers: {
              'X-OCR-SECRET': '' // Secret Key 
            }
          }
        )
        .then(res => {
          if (res.status === 200) {
            console.log('requestWithBase64 response:', res.data)
          }
        })
        .catch(e => {
          console.warn('requestWithBase64 error', e.response)
        })
    }
    
    function requestWithFile () {
      const file = '' // image file object. Example: fs.createReadStream('./example.png')
      const message = {
        images: [
          {
            format: '', // file format
            name: '' // file name
          }
        ],
        requestId: '', // unique string
        timestamp: 0,
        version: 'V2'
      }
      const formData = new FormData()
    
      formData.append('file', file)
      formData.append('message', JSON.stringify(message))
    
      axios
        .post(
          '', // APIGW Invoke URL
          formData,
          {
            headers: {
              'X-OCR-SECRET': '', // Secret Key 
              ...formData.getHeaders()
            }
          }
        )
        .then(res => {
          if (res.status === 200) {
            console.log('requestWithFile response:', res.data)
          }
        })
        .catch(e => {
          console.warn('requestWithFile error', e.response)
        })
    }
    

    Content-Type: application/json인 경우

    요청 헤더 Content-Typeapplication/json인 경우의 API 예제를 소개합니다.

    Java

    Java 기반의 API 예제 코드는 다음과 같습니다.

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.UUID;
    
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public class OCRGeneralAPIDemo {
    
    	public static void main(String[] args) {
    		String apiURL = "YOUR_API_URL";
    		String secretKey = "YOUR_SECRET_KEY";
    
    		try {
    			URL url = new URL(apiURL);
    			HttpURLConnection con = (HttpURLConnection)url.openConnection();
    			con.setUseCaches(false);
    			con.setDoInput(true);
    			con.setDoOutput(true);
    			con.setRequestMethod("POST");
    			con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    			con.setRequestProperty("X-OCR-SECRET", secretKey);
    
    			JSONObject json = new JSONObject();
    			json.put("version", "V2");
    			json.put("requestId", UUID.randomUUID().toString());
    			json.put("timestamp", System.currentTimeMillis());
    			JSONObject image = new JSONObject();
    			image.put("format", "jpg");
    			image.put("url", "https://kr.object.ncloudstorage.com/ocr-ci-test/sample/1.jpg"); // image should be public, otherwise, should use data
    			// FileInputStream inputStream = new FileInputStream("YOUR_IMAGE_FILE");
    			// byte[] buffer = new byte[inputStream.available()];
    			// inputStream.read(buffer);
    			// inputStream.close();
    			// image.put("data", buffer);
    			image.put("name", "demo");
    			JSONArray images = new JSONArray();
    			images.put(image);
    			json.put("images", images);
    			String postParams = json.toString();
    
    			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    			wr.writeBytes(postParams);
    			wr.flush();
    			wr.close();
    
    			int responseCode = con.getResponseCode();
    			BufferedReader br;
    			if (responseCode == 200) {
    				br = new BufferedReader(new InputStreamReader(con.getInputStream()));
    			} else {
    				br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
    			}
    			String inputLine;
    			StringBuffer response = new StringBuffer();
    			while ((inputLine = br.readLine()) != null) {
    				response.append(inputLine);
    			}
    			br.close();
    
    			System.out.println(response);
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    	}
    
    }
    
    

    Python

    Python 기반의 API 예제 코드는 다음과 같습니다.

    import requests
    import uuid
    import time
    import base64
    import json
    
    api_url = 'YOUR_API_URL'
    secret_key = 'YOUR_SECRET_KEY'
    image_url = 'YOUR_IMAGE_URL'
    # image_file = 'YOUR_IMAGE_FILE'
    # with open(image_file,'rb') as f:
    #     file_data = f.read()
    
    request_json = {
        'images': [
            {
                'format': 'jpg',
                'name': 'demo',
    #             'data': base64.b64encode(file_data).decode()
                'url': image_url
            }
        ],
        'requestId': str(uuid.uuid4()),
        'version': 'V2',
        'timestamp': int(round(time.time() * 1000))
    }
    
    payload = json.dumps(request_json).encode('UTF-8')
    headers = {
      'X-OCR-SECRET': secret_key,
      'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", api_url, headers=headers, data = payload)
    
    print(response.text)
    
    

    PHP

    PHP 기반의 API 예제 코드는 다음과 같습니다.

    <?php
      $client_secret = "YOUR_SECRET_KEY";
      $url = "YOUR_API_URL";
      $image_url = "YOUR_IMAGE_URL";
      // $image_file = "YOUR_IMAGE_FILE";
    
      $params->version = "V2";
      $params->requestId = "uuid";
      $params->timestamp = time();
      $image->format = "jpg";
      $image->url = $image_url;
      // $image->data = base64_encode(file_get_contents($image_file));
      $image->name = "demo";
      $images = array($image);
      $params->images = $images;
      $json = json_encode($params);
    
      $is_post = true;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, $is_post);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
      $headers = array();
      $headers[] = "X-OCR-SECRET: ".$client_secret;
      $headers[] = "Content-Type:application/json; charset=utf-8";
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      $response = curl_exec($ch);
      $err = curl_error($ch);
      $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close ($ch);
    
      echo $status_code;
      if($status_code == 200) {
        echo $response;
      } else {
        echo "ERROR: ".$response;
      }
    ?>
    
    

    ASP

    ASP 기반의 API 예제 코드는 다음과 같습니다.

    <%
    Function ASPPostJSON()
        Dim objXmlHttp
        Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
        url = "YOUR_INVOKE_URL"
        secret = "YOUR_SECRET_KEY"
        base64 = """"+ToBase64(getBinaryFile(Server.MapPath("test.jpg")))+""""
        body = "{""images"":[{""format"":""jpg"",""name"":""test 1"",""data"":"+base64+"}],""requestId"":""string"",""timestamp"":0,""version"":""V2"",""lang"":""ko""}"
        objXmlHttp.Open "POST", url, False
        objXmlHttp.SetRequestHeader "Content-Type", "application/json"
        objXmlHttp.SetRequestHeader "X-OCR-SECRET", secret
    
        objXmlHttp.Send body
    
        ASPPostJSON = CStr(objXmlHttp.ResponseText)
    
        Response.write(ASPPostJSON)
        Set objXmlHttp = Nothing
    
    End Function
    
    Function getBinaryFile(strFilePath)
      Dim TypeBinary, oStream
    
      TypeBinary = 1
    
      Set oStream = Server.CreateObject("ADODB.Stream")
    
      oStream.Open
    
      oStream.Type = TypeBinary
      oStream.LoadFromFile strFilePath
    
      getBinaryFile = oStream.read
    
      Set oStream = Nothing
    
    End Function
    
    Function ToBase64(rabyt)
    
         Dim xml: Set xml = CreateObject("MSXML2.DOMDocument.3.0")
         xml.LoadXml "<root />"
         xml.documentElement.dataType = "bin.base64"
         xml.documentElement.nodeTypedValue = rabyt
    
         ToBase64 = xml.documentElement.Text
    
    End Function
    
    call ASPPostJSON()
    %>
    

    この記事は役に立ちましたか?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.