Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- nama : ocr
- base : https://www.newocr.com
- by : wolep
- update : 1 Desember 10 PM
- node : v24.11.1
- note : idk, enjoy
- module : esm (tested) / cjs (gak tau coba test yah :v)
- */
- import fs from 'fs'
- const ocr = {
- baseUrl: 'https://www.newocr.com',
- baseHeaders: {
- 'Accept-Encoding': 'gzip, deflate, br, zstd',
- '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'
- },
- log: (message) => console.log('[ocr] ' + message),
- error: async (functionName, r) => `${functionName} ${r.status} ${r.statusText}\n${await r.text() || '(empty response)'}`,
- upload: async function (imageBuffer) {
- this.log('uploading')
- const headers = this.baseHeaders
- const body = new FormData();
- body.append('userfile', new Blob([imageBuffer]), 'image.jpeg');
- body.append('preview', '1');
- const r = await fetch(this.baseUrl, {
- headers, body,
- 'method': 'post'
- })
- if (!r.ok) throw Error(await this.error('upload', r))
- const html = await r.text()
- const u = html.match(/name ="u" value="(.+?)"/)?.[1]
- if (!u) throw Error(`gagal mendapatkan upload id`)
- return { u }
- },
- extract: async function (uobj) {
- this.log('extract text')
- const { u } = uobj
- const headers = {
- 'content-type': 'application/x-www-form-urlencoded',
- ...this.baseHeaders
- }
- const body = new URLSearchParams({
- "l3": "ind",
- "l2[]": "ind",
- "r": "0",
- "psm": "6",
- "u": u,
- //"x1": "0",
- //"x2": "533",
- //"y1": "0",
- //"y2": "700",
- "ocr": "1"
- })
- const r = await fetch(this.baseUrl, {
- headers, body,
- 'method': 'post'
- })
- if (!r.ok) throw Error(await this.error('extract', r))
- const html = await r.text()
- const text = html.match(/<textarea id="ocr-result"(?:.+?)>(.+?)<\/textarea>\n\n\t\t<\/div>/ms)?.[1]
- return text
- },
- read: async function(imageBuffer) {
- const uObj = await this.upload(imageBuffer)
- const extractObject = await this.extract(uObj)
- this.log('selesai')
- return extractObject
- }
- }
- // cara pakai
- const buffer = fs.readFileSync("./input.jpeg")
- ocr.read(buffer)
- .then(console.log)
- .catch(console.log)
- /* output
- string bla bla bla bla atau undefined kalau gagal membaca string
- */
Advertisement
Add Comment
Please, Sign In to add comment