wolfyflutter

70. www.newocr.com [ocr]

Dec 1st, 2025 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.61 KB | Source Code | 0 0
  1. /*
  2.   nama   : ocr
  3.   base   : https://www.newocr.com
  4.   by     : wolep
  5.   update : 1 Desember 10 PM
  6.   node   : v24.11.1
  7.   note   : idk, enjoy
  8.   module : esm (tested) / cjs (gak tau coba test yah :v)
  9. */
  10.  
  11. import fs from 'fs'
  12.  
  13. const ocr = {
  14.     baseUrl: 'https://www.newocr.com',
  15.     baseHeaders: {
  16.         'Accept-Encoding': 'gzip, deflate, br, zstd',
  17.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
  18.     },
  19.     log: (message) => console.log('[ocr] ' + message),
  20.     error: async (functionName, r) => `${functionName} ${r.status} ${r.statusText}\n${await r.text() || '(empty response)'}`,
  21.  
  22.     upload: async function (imageBuffer) {
  23.         this.log('uploading')
  24.         const headers = this.baseHeaders
  25.  
  26.         const body = new FormData();
  27.         body.append('userfile', new Blob([imageBuffer]), 'image.jpeg');
  28.         body.append('preview', '1');
  29.  
  30.         const r = await fetch(this.baseUrl, {
  31.             headers, body,
  32.             'method': 'post'
  33.         })
  34.         if (!r.ok) throw Error(await this.error('upload', r))
  35.  
  36.         const html = await r.text()
  37.         const u = html.match(/name ="u" value="(.+?)"/)?.[1]
  38.         if (!u) throw Error(`gagal mendapatkan upload id`)
  39.         return { u }
  40.     },
  41.  
  42.     extract: async function (uobj) {
  43.         this.log('extract text')
  44.         const { u } = uobj
  45.         const headers = {
  46.             'content-type': 'application/x-www-form-urlencoded',
  47.             ...this.baseHeaders
  48.         }
  49.  
  50.         const body = new URLSearchParams({
  51.             "l3": "ind",
  52.             "l2[]": "ind",
  53.             "r": "0",
  54.             "psm": "6",
  55.             "u": u,
  56.             //"x1": "0",
  57.             //"x2": "533",
  58.             //"y1": "0",
  59.             //"y2": "700",
  60.             "ocr": "1"
  61.         })
  62.  
  63.         const r = await fetch(this.baseUrl, {
  64.             headers, body,
  65.             'method': 'post'
  66.         })
  67.         if (!r.ok) throw Error(await this.error('extract', r))
  68.         const html = await r.text()
  69.         const text = html.match(/<textarea id="ocr-result"(?:.+?)>(.+?)<\/textarea>\n\n\t\t<\/div>/ms)?.[1]
  70.         return text
  71.     },
  72.  
  73.     read: async function(imageBuffer) {
  74.         const uObj = await this.upload(imageBuffer)
  75.         const extractObject = await this.extract(uObj)
  76.         this.log('selesai')
  77.         return extractObject
  78.     }
  79. }
  80.  
  81. // cara pakai
  82. const buffer = fs.readFileSync("./input.jpeg")
  83. ocr.read(buffer)
  84. .then(console.log)
  85. .catch(console.log)
  86.  
  87. /* output
  88. string bla bla bla bla atau undefined kalau gagal membaca string
  89. */
  90.  
Tags: ocr
Advertisement
Add Comment
Please, Sign In to add comment