package com.bellkenz.irisocr import android.graphics.Bitmap import android.graphics.BitmapFactory import android.util.Base64 import com.getcapacitor.JSObject import com.getcapacitor.Plugin import com.getcapacitor.PluginCall import com.getcapacitor.PluginMethod import com.getcapacitor.annotation.CapacitorPlugin import com.google.mlkit.vision.common.InputImage import com.google.mlkit.vision.text.TextRecognition import com.google.mlkit.vision.text.latin.TextRecognizerOptions /** * On-device OCR using Google ML Kit's text recognition. The image is decoded * from the base64 the web layer passes, recognized ON THE DEVICE (ML Kit's * on-device model — no network call), and only the resulting text is returned. * The image is never written to disk and never uploaded (IRIS Chat invariant #8). * The returned text is masked on-device by the web layer before anything else. */ @CapacitorPlugin(name = "IrisOcr") class IrisOcrPlugin : Plugin() { @PluginMethod fun recognizeText(call: PluginCall) { val b64 = call.getString("image") if (b64 == null) { call.reject("No image provided") return } val bytes = try { Base64.decode(b64, Base64.DEFAULT) } catch (e: Exception) { call.reject("Could not decode the image") return } val bitmap: Bitmap? = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) if (bitmap == null) { call.reject("Could not decode the image") return } val image = InputImage.fromBitmap(bitmap, 0) val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS) recognizer.process(image) .addOnSuccessListener { result -> val ret = JSObject() ret.put("text", result.text) call.resolve(ret) } .addOnFailureListener { e -> call.reject("OCR failed: ${e.message}") } } }