跪拜 Guibai
← All articles
HarmonyOS

Before Upload: On-Device Scan Flags ID Cards, Faces, and Phone Numbers in Images

By 李小轰_Rex ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Apps can now tell users what is in an image before any bytes reach a server, so privacy screening does not create a second leak. For HarmonyOS upload flows, the default changes from "user uploads, server stores original" to "device flags, user decides".

Summary

Uploaded screenshots and photos routinely carry ID cards, phone numbers, and faces. @ohos/ai-privacy-scan adds a pre-upload check that keeps the analysis on the device: OCR reads all text, face detection locates faces, and entity recognition pulls phone numbers, ID numbers, emails, URLs, names, locations, times, OTP codes, tracking numbers, flight numbers, and passports from the recognized text. Hits come back as RiskItem[] with bounding boxes, and the original pixels are never modified or sent to a server for inspection.

The library wraps three built-in HarmonyOS kits: Core Vision Kit for textRecognition and faceDetector, and Natural Language Kit for getEntity. Callers choose what to scan through PrivacyType enums, from FACE and PHONE to IDCARD, BANKCARD, EXPRESS, FLIGHT, PASSPORT, or ALL. A showWindow flag controls a confirmation dialog: true pops a "sensitive information" window with boxes and a scrollable list when hits exist; false delivers the list directly to onConfirm. If a scan fails partway, onError fires instead of returning an empty list that could be mistaken for a clean result.

Takeaways
— @ohos/ai-privacy-scan is available on GitHub and GitCode.
— The pre-check uses Core Vision Kit textRecognition and faceDetector plus Natural Language Kit getEntity, all running on the device.
— Supported PrivacyType values are ALL, FACE, PHONE, LOCATION, DATETIME, IDCARD, EMAIL, BANKCARD, URL, NAME, OTP, EXPRESS, FLIGHT, and PASSPORT.
— An empty types array triggers onError with "Please select at least one pre-check type".
— ALL in the types array checks every built-in type; only FACE skips OCR, and omitting FACE skips face detection.
— showWindow true shows a confirmation dialog only when hits exist, and tapping the mask does not dismiss it; showWindow false hands RiskItem[] to onConfirm as soon as recognition finishes.
— RiskItem carries type, text, and level, plus an optional box with x, y, width, and height in pixels from the top-left of the original image; items without a determinable position have no box.
— An empty RiskItem[] means the scan completed and found nothing; a failed scan calls onError instead.
— No image pixels are altered and no upload is blocked; the caller decides how to use the hit list.
Conclusions

Keeping the analysis on-device is a deliberate privacy boundary; the original image never reaches a server just to be checked, so the scan itself cannot leak the data it is meant to protect.

Returning an error on an incomplete scan instead of an empty result prevents a false "all clear"; a privacy check that reported no hits after a failed run would be worse than no check.

The library intentionally stops at warning. Blurring and upload decisions stay with the caller, which keeps it useful for legitimate uploads of sensitive documents.

Concepts & terms
On-device AI
AI inference that runs locally on the device instead of sending data to a cloud service. In this pre-check, OCR, face detection, and entity extraction all happen without the original image leaving the device.
Entity recognition
A natural language processing step that extracts structured items from text, such as phone numbers, ID numbers, names, locations, and times. HarmonyOS exposes it through Natural Language Kit's getEntity.
PixelMap
HarmonyOS's in-memory image representation. The check expects a PixelMap decoded as RGBA_8888, with 8 bits for each of the red, green, blue, and alpha channels.
RiskItem
The result object returned by the scan. type identifies the privacy category, text is the matched content, level is a severity rating, and box gives pixel coordinates on the original image when available.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗