1
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
|
||||
package="io.dcloud.uni_modules.xVibrateS">
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> -->
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"minSdkVersion": "20",
|
||||
"dependencies":[
|
||||
"com.google.mlkit:text-recognition:16.0.0",
|
||||
"com.google.mlkit:text-recognition-chinese:16.0.0",
|
||||
"com.google.mlkit:text-recognition-devanagari:16.0.0",
|
||||
"com.google.mlkit:text-recognition-japanese:16.0.0",
|
||||
"com.google.mlkit:text-recognition-korean:16.0.0"
|
||||
],
|
||||
"abis": ["armeabi-v7a", "arm64-v8a"]
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
import Text from "com.google.mlkit.vision.text.Text"
|
||||
import TextRecognition from "com.google.mlkit.vision.text.TextRecognition"
|
||||
import TextRecognizer from "com.google.mlkit.vision.text.TextRecognizer"
|
||||
import TextRecognizerOptionsInterface from "com.google.mlkit.vision.text.TextRecognizerOptionsInterface"
|
||||
import ChineseTextRecognizerOptions from "com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions"
|
||||
import JapaneseTextRecognizerOptions from "com.google.mlkit.vision.text.japanese.JapaneseTextRecognizerOptions"
|
||||
|
||||
import Context from 'android.content.Context'
|
||||
import IOException from 'java.io.IOException'
|
||||
import InputStream from 'java.io.InputStream'
|
||||
import Uri from 'android.net.Uri';
|
||||
import File from 'java.io.File'
|
||||
import Bitmap from "android.graphics.Bitmap"
|
||||
import BarcodeScanner from "com.google.mlkit.vision.barcode.BarcodeScanner"
|
||||
import BarcodeScannerOptions from "com.google.mlkit.vision.barcode.BarcodeScannerOptions"
|
||||
import BarcodeScanning from "com.google.mlkit.vision.barcode.BarcodeScanning"
|
||||
import ZoomSuggestionOptions from "com.google.mlkit.vision.barcode.ZoomSuggestionOptions"
|
||||
import ZoomCallback from "com.google.mlkit.vision.barcode.ZoomSuggestionOptions.ZoomCallback"
|
||||
import Barcode from "com.google.mlkit.vision.barcode.common.Barcode"
|
||||
import InputImage from "com.google.mlkit.vision.common.InputImage"
|
||||
import Task from "com.google.android.gms.tasks.Task"
|
||||
import List from "java.util.List"
|
||||
import ByteBuffer from 'java.nio.ByteBuffer'
|
||||
import ImageFormat from 'android.graphics.ImageFormat'
|
||||
|
||||
import Vibrator from "android.os.Vibrator"
|
||||
|
||||
|
||||
import ContentValues from "android.content.ContentValues"
|
||||
import ContentUris from "android.content.ContentUris"
|
||||
import JSONObject from "org.json.JSONObject"
|
||||
import TimeZone from "java.util.TimeZone"
|
||||
import Cursor from "android.database.Cursor"
|
||||
import MediaStore from "android.provider.MediaStore"
|
||||
import SimpleDateFormat from "java.text.SimpleDateFormat"
|
||||
import Locale from "java.util.Locale"
|
||||
import MediaScannerConnection from "android.media.MediaScannerConnection"
|
||||
|
||||
|
||||
type callType = (list:any)=> void;
|
||||
|
||||
/**
|
||||
* 震动
|
||||
* @param {number} duriation 震动时间单位ms
|
||||
*/
|
||||
export function vibrator(duriation : number) : boolean {
|
||||
try {
|
||||
const context = UTSAndroid.getAppContext() as Context
|
||||
let vb = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator;
|
||||
if (vb!.hasVibrator()) {
|
||||
vb!.vibrate(duriation.toLong());
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从相册/相机中选取图片进行识别
|
||||
*/
|
||||
export function chooseImageBuilder(language:string|null) : Promise<string[]> {
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
let local = language == null?'zh':language!
|
||||
return new Promise((res, rej) => {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success(evt : ChooseImageSuccess) {
|
||||
uni.showLoading({ title: '...', mask: true })
|
||||
if (evt.tempFilePaths.length > 0) {
|
||||
const imgs = evt.tempFilePaths[0] as string;
|
||||
try {
|
||||
const decodedPath = Uri.decode(imgs).substring(7)
|
||||
const file = File(decodedPath)
|
||||
let image = InputImage.fromFilePath(UTSAndroid.getAppContext() as Context, Uri.fromFile(file));
|
||||
let opts = new ChineseTextRecognizerOptions().Builder()
|
||||
// if(local == 'ja'){
|
||||
// opts = new JapaneseTextRecognizerOptions().Builder()
|
||||
// }
|
||||
const recognizer = TextRecognition.getClient(opts.build()) as TextRecognizer;
|
||||
recognizer.process(image!)
|
||||
.addOnSuccessListener((TextList) => {
|
||||
let str = [] as string[]
|
||||
for (block in TextList.textBlocks) {
|
||||
for (line in block.lines) {
|
||||
// let lineText = line.text
|
||||
// let lineCornerPoints = line.cornerPoints
|
||||
// let lineFrame = line.boundingBox
|
||||
for (element in line.elements) {
|
||||
if (element.getConfidence() >= zxd) {
|
||||
let elementText = element.text as string;
|
||||
// let elementCornerPoints = element.cornerPoints
|
||||
// let elementFrame = element.boundingBox
|
||||
str.push(elementText as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
vibrator(100)
|
||||
res(str as string[])
|
||||
})
|
||||
.addOnFailureListener((e) => {
|
||||
uni.hideLoading()
|
||||
rej(["识别失败"] as string[])
|
||||
})
|
||||
|
||||
|
||||
} catch (e : IOException) {
|
||||
uni.hideLoading()
|
||||
rej(["图片选取失败"] as string[])
|
||||
}
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
rej(["图片选择失败"] as string[])
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供一个远程图片地址进行识别
|
||||
*/
|
||||
export function downloadUrlImageBuilder(url : string,language:string|null) : Promise<string[]> {
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
let local = language == null?'zh':language!
|
||||
uni.showLoading({ title: '...', mask: true })
|
||||
return new Promise((res, rej) => {
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success(evt) {
|
||||
const contentResolver = UTSAndroid.getAppContext()!.contentResolver
|
||||
let decodedPath = evt.tempFilePath
|
||||
if (evt.tempFilePath.indexOf('file://') > -1) {
|
||||
decodedPath = Uri.decode(evt.tempFilePath).substring(7)
|
||||
}
|
||||
|
||||
try {
|
||||
const file = File(decodedPath)
|
||||
let image = InputImage.fromFilePath(UTSAndroid.getAppContext() as Context, Uri.fromFile(file));
|
||||
console.log(typeof ChineseTextRecognizerOptions)
|
||||
let opts = new ChineseTextRecognizerOptions().Builder()
|
||||
// if(local == 'ja'){
|
||||
// opts = new JapaneseTextRecognizerOptions().Builder()
|
||||
// }
|
||||
|
||||
const recognizer = TextRecognition.getClient(opts.build()) as TextRecognizer;
|
||||
if (image != null) {
|
||||
const result = recognizer.process(image!)
|
||||
.addOnSuccessListener((TextList) => {
|
||||
let str = [] as string[]
|
||||
for (block in TextList.textBlocks) {
|
||||
for (line in block.lines) {
|
||||
// let lineText = line.text
|
||||
// let lineCornerPoints = line.cornerPoints
|
||||
// let lineFrame = line.boundingBox
|
||||
for (element in line.elements) {
|
||||
if (element.getConfidence() >= zxd) {
|
||||
let elementText = element.text as string;
|
||||
// let elementCornerPoints = element.cornerPoints
|
||||
// let elementFrame = element.boundingBox
|
||||
str.push(elementText as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
vibrator(100)
|
||||
res(str as string[])
|
||||
})
|
||||
.addOnFailureListener((e) => {
|
||||
uni.hideLoading()
|
||||
rej(["识别失败"] as string[])
|
||||
})
|
||||
} else {
|
||||
uni.hideLoading()
|
||||
rej(["图片下载失败"])
|
||||
}
|
||||
} catch (e : IOException) {
|
||||
uni.hideLoading()
|
||||
rej(["图片下载失败"])
|
||||
}
|
||||
|
||||
},
|
||||
fail() {
|
||||
uni.hideLoading()
|
||||
rej(["图片下载失败"])
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定图片路径进行识别
|
||||
*/
|
||||
export function localFilePathImageBuilder(pathfile : string,language:string|null) : Promise<string[]> {
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
let local = language == null?'zh':language!
|
||||
uni.showLoading({ title: '...', mask: true })
|
||||
return new Promise((res, rej) => {
|
||||
try {
|
||||
let decodedPath = pathfile
|
||||
if (pathfile.indexOf('file://') > -1) {
|
||||
decodedPath = Uri.decode(pathfile).substring(7)
|
||||
}
|
||||
const file = File(decodedPath)
|
||||
let image = InputImage.fromFilePath(UTSAndroid.getAppContext() as Context, Uri.fromFile(file));
|
||||
let opts = new ChineseTextRecognizerOptions().Builder()
|
||||
// if(local == 'ja'){
|
||||
// opts = new JapaneseTextRecognizerOptions().Builder()
|
||||
// }
|
||||
const recognizer = TextRecognition.getClient(opts.build()) as TextRecognizer;
|
||||
if (image != null) {
|
||||
const result = recognizer.process(image!)
|
||||
.addOnSuccessListener((TextList) => {
|
||||
let str = [] as string[]
|
||||
for (block in TextList.textBlocks) {
|
||||
for (line in block.lines) {
|
||||
// let lineText = line.text
|
||||
// let lineCornerPoints = line.cornerPoints
|
||||
// let lineFrame = line.boundingBox
|
||||
for (element in line.elements) {
|
||||
if (element.getConfidence() >= zxd) {
|
||||
let elementText = element.text as string;
|
||||
// let elementCornerPoints = element.cornerPoints
|
||||
// let elementFrame = element.boundingBox
|
||||
str.push(elementText as string)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
vibrator(100)
|
||||
res(str as string[])
|
||||
})
|
||||
.addOnFailureListener((e) => {
|
||||
uni.hideLoading()
|
||||
rej(["识别失败"] as string[])
|
||||
})
|
||||
} else {
|
||||
uni.hideLoading()
|
||||
rej(["图片地址解析失败"])
|
||||
}
|
||||
} catch (e : IOException) {
|
||||
uni.hideLoading()
|
||||
rej(["图片地址解析失败"])
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
import Text from "com.google.mlkit.vision.text.Text"
|
||||
import TextRecognition from "com.google.mlkit.vision.text.TextRecognition"
|
||||
import TextRecognizer from "com.google.mlkit.vision.text.TextRecognizer"
|
||||
import TextRecognizerOptionsInterface from "com.google.mlkit.vision.text.TextRecognizerOptionsInterface"
|
||||
import ChineseTextRecognizerOptions from "com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions"
|
||||
import JapaneseTextRecognizerOptions from "com.google.mlkit.vision.text.japanese.JapaneseTextRecognizerOptions"
|
||||
|
||||
import Context from 'android.content.Context'
|
||||
import IOException from 'java.io.IOException'
|
||||
import InputStream from 'java.io.InputStream'
|
||||
import Uri from 'android.net.Uri';
|
||||
import File from 'java.io.File'
|
||||
import Bitmap from "android.graphics.Bitmap"
|
||||
import BarcodeScanner from "com.google.mlkit.vision.barcode.BarcodeScanner"
|
||||
import BarcodeScannerOptions from "com.google.mlkit.vision.barcode.BarcodeScannerOptions"
|
||||
import BarcodeScanning from "com.google.mlkit.vision.barcode.BarcodeScanning"
|
||||
import ZoomSuggestionOptions from "com.google.mlkit.vision.barcode.ZoomSuggestionOptions"
|
||||
import ZoomCallback from "com.google.mlkit.vision.barcode.ZoomSuggestionOptions.ZoomCallback"
|
||||
import Barcode from "com.google.mlkit.vision.barcode.common.Barcode"
|
||||
import InputImage from "com.google.mlkit.vision.common.InputImage"
|
||||
import Task from "com.google.android.gms.tasks.Task"
|
||||
import List from "java.util.List"
|
||||
import ByteBuffer from 'java.nio.ByteBuffer'
|
||||
import ImageFormat from 'android.graphics.ImageFormat'
|
||||
|
||||
import Vibrator from "android.os.Vibrator"
|
||||
|
||||
|
||||
import ContentValues from "android.content.ContentValues"
|
||||
import ContentUris from "android.content.ContentUris"
|
||||
import JSONObject from "org.json.JSONObject"
|
||||
import TimeZone from "java.util.TimeZone"
|
||||
import Cursor from "android.database.Cursor"
|
||||
import MediaStore from "android.provider.MediaStore"
|
||||
import SimpleDateFormat from "java.text.SimpleDateFormat"
|
||||
import Locale from "java.util.Locale"
|
||||
import MediaScannerConnection from "android.media.MediaScannerConnection"
|
||||
import Rect from 'android.graphics.Rect';
|
||||
|
||||
|
||||
type callType = (list:any)=> void;
|
||||
|
||||
/**
|
||||
* 震动
|
||||
* @param {number} duriation 震动时间单位ms
|
||||
*/
|
||||
export function vibrator(duriation : number) : boolean {
|
||||
try {
|
||||
const context = UTSAndroid.getAppContext() as Context
|
||||
let vb = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator;
|
||||
if (vb!.hasVibrator()) {
|
||||
vb!.vibrate(duriation.toLong());
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从相册/相机中选取图片进行识别
|
||||
* @param callback {(string[],any[])=>{}} 回调参数:第一个是识别的文本数组,第二个是源数据{boundingBox:[x,y,width,height],text:elementText}[]字符串,需要JSON.pare,携带了坐标等详细资料
|
||||
* @param language {string} 需要识别的语言 zh中文,ja日文
|
||||
*/
|
||||
export function chooseImageBuilder(callback:(str:string[],source:string[])=>void,langs:string|null){
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success(evt : ChooseImageSuccess) {
|
||||
uni.showLoading({ title: '...', mask: true })
|
||||
if (evt.tempFilePaths.length > 0) {
|
||||
const imgs = evt.tempFilePaths[0] as string;
|
||||
try {
|
||||
const decodedPath = UTSAndroid.convert2AbsFullPath(imgs)
|
||||
const file = new File(decodedPath)
|
||||
let image = InputImage.fromFilePath(UTSAndroid.getAppContext() as Context, Uri.fromFile(file));
|
||||
|
||||
let recognizer = TextRecognition.getClient(new ChineseTextRecognizerOptions.Builder().build()) as TextRecognizer;
|
||||
if(langs=='ja'){
|
||||
recognizer = TextRecognition.getClient(new JapaneseTextRecognizerOptions.Builder().build()) as TextRecognizer;
|
||||
}
|
||||
recognizer.process(image!)
|
||||
.addOnSuccessListener((TextList) => {
|
||||
let str = [] as string[]
|
||||
let sour = [] as string[]
|
||||
for (block in TextList.textBlocks) {
|
||||
for (line in block.lines) {
|
||||
// let lineText = line.text
|
||||
// let lineCornerPoints = line.cornerPoints
|
||||
// let lineFrame = line.getBoundingBox
|
||||
let texts = ''
|
||||
for (element in line.elements) {
|
||||
if (element.getConfidence() >= zxd) {
|
||||
let elementText = element.text as string;
|
||||
|
||||
let elementFrame = element.getBoundingBox()
|
||||
let rect = elementFrame as Rect
|
||||
sour.push(JSON.stringify({boundingBox:[rect.left,rect.top,rect.width(),rect.height()],text:elementText} as UTSJSONObject))
|
||||
texts+=elementText
|
||||
|
||||
}
|
||||
}
|
||||
if(texts!=''){
|
||||
str.push(texts)
|
||||
}
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
vibrator(100)
|
||||
callback(str as string[],sour as string[])
|
||||
})
|
||||
.addOnFailureListener((e) => {
|
||||
|
||||
callback([] as string[],[] as string[])
|
||||
})
|
||||
|
||||
|
||||
} catch (e : IOException) {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 从相册/相机中选取图片进行识别
|
||||
* @param url {string} 图片地址.
|
||||
* @param callback {(string[],any[])=>{}} 回调参数:第一个是识别的文本数组,第二个是源数据{boundingBox:[x,y,width,height],text:elementText}[]字符串,需要JSON.pare,携带了坐标等详细资料
|
||||
* @param language {string} 需要识别的语言 zh中文,ja日文
|
||||
*/
|
||||
export function downloadUrlImageBuilder(url : string,callback:(str:string[],source:string[])=>void,langs:string|null) {
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success(evt) {
|
||||
const contentResolver = UTSAndroid.getAppContext()!.contentResolver
|
||||
let decodedPath = UTSAndroid.convert2AbsFullPath(evt.tempFilePath)
|
||||
|
||||
try {
|
||||
const file = new File(decodedPath)
|
||||
let image = InputImage.fromFilePath(UTSAndroid.getAppContext() as Context, Uri.fromFile(file));
|
||||
let recognizer = TextRecognition.getClient(new ChineseTextRecognizerOptions.Builder().build()) as TextRecognizer;
|
||||
if(langs=='ja'){
|
||||
recognizer = TextRecognition.getClient(new JapaneseTextRecognizerOptions.Builder().build()) as TextRecognizer;
|
||||
}
|
||||
if (image != null) {
|
||||
const result = recognizer.process(image!)
|
||||
.addOnSuccessListener((TextList) => {
|
||||
let str = [] as string[]
|
||||
let sour = [] as string[]
|
||||
for (block in TextList.textBlocks) {
|
||||
for (line in block.lines) {
|
||||
let texts = ''
|
||||
for (element in line.elements) {
|
||||
if (element.getConfidence() >= zxd) {
|
||||
let elementText = element.text as string;
|
||||
|
||||
let elementFrame = element.getBoundingBox()
|
||||
let rect = elementFrame as Rect
|
||||
sour.push(JSON.stringify({boundingBox:[rect.left,rect.top,rect.width(),rect.height()],text:elementText} as UTSJSONObject))
|
||||
texts+=elementText
|
||||
|
||||
}
|
||||
}
|
||||
if(texts!=''){
|
||||
str.push(texts)
|
||||
}
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
vibrator(100)
|
||||
callback(str as string[],sour)
|
||||
})
|
||||
.addOnFailureListener((e) => {
|
||||
callback([] as string[],[] as string[])
|
||||
})
|
||||
} else {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
} catch (e : IOException) {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
|
||||
},
|
||||
fail() {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从相册/相机中选取图片进行识别
|
||||
* @param pathfile {string} 图片地址.
|
||||
* @param callback {(string[],any[])=>{}} 回调参数:第一个是识别的文本数组,第二个是源数据{boundingBox:[x,y,width,height],text:elementText}[]字符串,需要JSON.pare,携带了坐标等详细资料
|
||||
* @param language {string} 需要识别的语言 zh中文,ja日文
|
||||
*/
|
||||
export function localFilePathImageBuilder(pathfile : string,callback:(str:string[],source:string[])=>void,langs:string|null){
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
try {
|
||||
let decodedPath = UTSAndroid.convert2AbsFullPath(pathfile)
|
||||
|
||||
const file = new File(decodedPath)
|
||||
let image = InputImage.fromFilePath(UTSAndroid.getAppContext() as Context, Uri.fromFile(file));
|
||||
let recognizer = TextRecognition.getClient(new ChineseTextRecognizerOptions.Builder().build()) as TextRecognizer;
|
||||
if(langs=='ja'){
|
||||
recognizer = TextRecognition.getClient(new JapaneseTextRecognizerOptions.Builder().build()) as TextRecognizer;
|
||||
}
|
||||
|
||||
if (image != null) {
|
||||
const result = recognizer.process(image!)
|
||||
.addOnSuccessListener((TextList) => {
|
||||
let str = [] as string[]
|
||||
let sour = [] as string[]
|
||||
for (block in TextList.textBlocks) {
|
||||
for (line in block.lines) {
|
||||
let texts = ''
|
||||
for (element in line.elements) {
|
||||
if (element.getConfidence() >= zxd) {
|
||||
let elementText = element.text as string;
|
||||
|
||||
let elementFrame = element.getBoundingBox()
|
||||
let rect = elementFrame as Rect
|
||||
sour.push(JSON.stringify({boundingBox:[rect.left,rect.top,rect.width(),rect.height()],text:elementText} as UTSJSONObject))
|
||||
texts+=elementText
|
||||
|
||||
}
|
||||
}
|
||||
if(texts!=''){
|
||||
str.push(texts)
|
||||
}
|
||||
}
|
||||
}
|
||||
callback(str as string[],sour)
|
||||
})
|
||||
.addOnFailureListener((e) => {
|
||||
callback([] as string[],[] as string[])
|
||||
})
|
||||
} else {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
} catch (e : IOException) {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"x_ocr_s": "./lib/x_ocr_s.har"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { x_ocrDecode } from "x_ocr_s"
|
||||
|
||||
export function chooseImageBuilder(callback : (str : string[], source : string[]) => void, langs : string | null) {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success(evt : ChooseImageSuccess) {
|
||||
uni.showLoading({ title: '', mask: true })
|
||||
if (evt.tempFilePaths.length > 0) {
|
||||
const imgs = UTSHarmony.convert2AbsFullPath(evt.tempFilePaths[0] as string);
|
||||
x_ocrDecode(imgs, (arg1 : string[], arg2 : string[]) => {
|
||||
callback(arg1, arg2)
|
||||
uni.hideLoading()
|
||||
})
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
callback([] as string[], [] as string[])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function downloadUrlImageBuilder(url : string,callback:(str:string[],source:string[])=>void,langs:string|null) {
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success(evt) {
|
||||
x_ocrDecode(UTSHarmony.convert2AbsFullPath(evt.tempFilePath), callback)
|
||||
},
|
||||
fail() {
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function localFilePathImageBuilder(pathfile : string,callback:(str:string[],source:string[])=>void,langs:string|null){
|
||||
x_ocrDecode(UTSHarmony.convert2AbsFullPath(pathfile), callback)
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"deploymentTarget": "12",
|
||||
"dependencies-pods": [
|
||||
{
|
||||
"name": "GoogleMLKit/TextRecognition",
|
||||
"version": "3.2.0"
|
||||
},
|
||||
{
|
||||
"name": "GoogleMLKit/TextRecognitionChinese",
|
||||
"version": "3.2.0"
|
||||
},
|
||||
{
|
||||
"name": "GoogleMLKit/TextRecognitionJapanese",
|
||||
"version": "3.2.0"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
import {ChineseTextRecognizerOptions,JapaneseTextRecognizerOptions,TextRecognizer,VisionImage,TextBlock,Text} from "MLKit"
|
||||
import * as UIKit from "UIKit"
|
||||
import { UIAlertController , UIAlertAction , UITextField,UIImage } from "UIKit"
|
||||
import { UTSiOS } from "DCloudUTSFoundation"
|
||||
import {String} from "Swift"
|
||||
import { PHPhotoLibrary } from "Photos"
|
||||
import { CGPoint , CGRect } from 'CoreFoundation';
|
||||
|
||||
|
||||
/**
|
||||
* 从相册/相机中选取图片进行识别
|
||||
* @param callback {(string[],string[])=>{}} 回调参数:第一个是识别的文本数组,第二个是源数据{boundingBox:[x,y,width,height],text:elementText}[]字符串,需要JSON.pare,携带了坐标等详细资料
|
||||
* @param language {string} 需要识别的语言 zh中文,ja日文
|
||||
*/
|
||||
export function chooseImageBuilder(callback:(str:string[],source:string[])=>void,langs:string|null){
|
||||
// 置信度
|
||||
let zxd = 0.50
|
||||
let local = 'zh'
|
||||
if(langs=='ja'){
|
||||
local = 'ja'
|
||||
}
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success:(evt : ChooseImageSuccess)=> {
|
||||
uni.showLoading({title:'...',mask:true})
|
||||
let chineseOptions = new ChineseTextRecognizerOptions()
|
||||
|
||||
let chineseTextRecognizer = TextRecognizer.textRecognizer(options=chineseOptions)
|
||||
if(local == 'ja'){
|
||||
chineseTextRecognizer = TextRecognizer.textRecognizer(options=new JapaneseTextRecognizerOptions())
|
||||
}
|
||||
if (evt.tempFilePaths.length > 0) {
|
||||
let imgs = evt.tempFilePaths[0] as string;
|
||||
try {
|
||||
let realImgPath = UTSiOS.convert2AbsFullPath(imgs)
|
||||
|
||||
let img = new UIImage(contentsOfFile = realImgPath);
|
||||
if(img!=null){
|
||||
let image = new VisionImage(image = img!);
|
||||
chineseTextRecognizer.process(image as VisionImage,completion=(TextList,error)=>{
|
||||
if(error!=null||TextList==null){
|
||||
uni.hideLoading()
|
||||
uni.showToast({title:"失败",icon:'none'})
|
||||
return;
|
||||
}
|
||||
let str = [] as string[]
|
||||
let sour = [] as string[]
|
||||
for(block in TextList!.blocks){
|
||||
let lines = block.lines
|
||||
if(lines!=null){
|
||||
for(line in lines) {
|
||||
let elements = line.elements
|
||||
|
||||
if(elements!=null){
|
||||
let texts = ''
|
||||
for(element in elements) {
|
||||
let elementText = element.text;
|
||||
let rect = element.frame as CGRect
|
||||
let elementFrame = [rect.origin.x,rect.origin.y,rect.size.width,rect.size.height] as number[]
|
||||
sour.push(JSON.stringify({boundingBox:elementFrame,text:elementText})!)
|
||||
texts+=elementText
|
||||
}
|
||||
if(texts!=''){
|
||||
str.push(texts)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
uni.hideLoading()
|
||||
callback(str as string[],sour)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.warn("图片选择失败")
|
||||
uni.hideLoading()
|
||||
callback([] as string[],[] as string[])
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
console.warn("图片选择失败")
|
||||
uni.showToast({title:"图片选择失败",icon:'none'})
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
/**
|
||||
* 从相册/相机中选取图片进行识别
|
||||
* @param url {string} 图片地址.
|
||||
* @param callback {(string[],string[])=>{}} 回调参数:第一个是识别的文本数组,第二个是源数据{boundingBox:[x,y,width,height],text:elementText}[]字符串,需要JSON.pare,携带了坐标等详细资料
|
||||
* @param language {string} 需要识别的语言 zh中文,ja日文
|
||||
*/
|
||||
export function downloadUrlImageBuilder(url : string,callback:(str:string[],source:string[])=>void,langs:string|null){
|
||||
uni.showLoading({title:'...',mask:true})
|
||||
let local = langs == null?'zh':(langs! as string)
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success:(evt)=> {
|
||||
let chineseOptions = new ChineseTextRecognizerOptions()
|
||||
let chineseTextRecognizer = TextRecognizer.textRecognizer(options=chineseOptions)
|
||||
|
||||
if(local == 'ja'){
|
||||
chineseTextRecognizer = TextRecognizer.textRecognizer(options=new JapaneseTextRecognizerOptions())
|
||||
}
|
||||
let imgs = evt.tempFilePath as string;
|
||||
|
||||
try {
|
||||
let realImgPath = UTSiOS.convert2AbsFullPath(imgs)
|
||||
let img = new UIImage(contentsOfFile = realImgPath);
|
||||
|
||||
if(img!=null){
|
||||
let image = new VisionImage(image = img!);
|
||||
chineseTextRecognizer.process(image as VisionImage,completion=(TextList,error)=>{
|
||||
if(error!=null||TextList==null){
|
||||
uni.hideLoading()
|
||||
uni.showToast({title:"失败",icon:'none'})
|
||||
return;
|
||||
}
|
||||
let str = [] as string[]
|
||||
let sour = [] as string[]
|
||||
for(block in TextList!.blocks){
|
||||
let lines = block.lines
|
||||
if(lines!=null){
|
||||
for(line in lines) {
|
||||
let elements = line.elements
|
||||
if(elements!=null){
|
||||
let texts = ''
|
||||
for(element in elements) {
|
||||
let elementText = element.text;
|
||||
let rect = element.frame as CGRect
|
||||
let elementFrame = [rect.origin.x,rect.origin.y,rect.size.width,rect.size.height] as number[]
|
||||
sour.push(JSON.stringify({boundingBox:elementFrame,text:elementText})!)
|
||||
|
||||
texts+=elementText
|
||||
}
|
||||
if(texts!=''){
|
||||
str.push(texts)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
uni.hideLoading()
|
||||
callback(str as string[],sour)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.warn("下载失败")
|
||||
uni.hideLoading()
|
||||
uni.showToast({title:"下载失败",icon:'none'})
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
|
||||
},
|
||||
fail() {
|
||||
uni.hideLoading()
|
||||
uni.showToast({title:"下载失败",icon:'none'})
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
})
|
||||
}
|
||||
export function localFilePathImageBuilder(pathfile : string,callback:(str:string[],source:string[])=>void,langs:string|null){
|
||||
let local = langs == null?'zh':(langs! as string)
|
||||
let chineseOptions = new ChineseTextRecognizerOptions()
|
||||
let chineseTextRecognizer = TextRecognizer.textRecognizer(options=chineseOptions)
|
||||
|
||||
if(local == 'ja'){
|
||||
chineseTextRecognizer = TextRecognizer.textRecognizer(options=new JapaneseTextRecognizerOptions())
|
||||
}
|
||||
let realImgPath = UTSiOS.convert2AbsFullPath(pathfile)
|
||||
let img = new UIImage(contentsOfFile = realImgPath);
|
||||
|
||||
if(img!=null){
|
||||
let image = new VisionImage(image = img!);
|
||||
chineseTextRecognizer.process(image as VisionImage,completion=(TextList,error)=>{
|
||||
if(error!=null||TextList==null){
|
||||
callback([] as string[],[] as string[])
|
||||
return;
|
||||
}
|
||||
let str = [] as string[]
|
||||
let sour = [] as string[]
|
||||
for(block in TextList!.blocks){
|
||||
let lines = block.lines
|
||||
if(lines!=null){
|
||||
for(line in lines) {
|
||||
let elements = line.elements
|
||||
if(elements!=null){
|
||||
let texts = ''
|
||||
for(element in elements) {
|
||||
let elementText = element.text;
|
||||
let rect = element.frame as CGRect
|
||||
let elementFrame = [rect.origin.x,rect.origin.y,rect.size.width,rect.size.height] as number[]
|
||||
sour.push(JSON.stringify({boundingBox:elementFrame,text:elementText})!)
|
||||
|
||||
texts+=elementText
|
||||
}
|
||||
if(texts!=''){
|
||||
str.push(texts)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
callback(str as string[],sour)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPhotoLibraryUsageDescription</key>
|
||||
<string>读取您的相册图片,用来识别图片中的文字功能,如果拒绝将无法使用相关识别功能.</string>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,103 @@
|
||||
async function loadScript(url) {
|
||||
return new Promise(res=>{
|
||||
var script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.src = url;
|
||||
|
||||
// 当脚本加载完成后,执行回调
|
||||
script.onload = function() {
|
||||
res()
|
||||
};
|
||||
|
||||
// 处理旧版浏览器的onreadystatechange事件
|
||||
script.onreadystatechange = function() {
|
||||
if (this.readyState === 'loaded' || this.readyState === 'complete') {
|
||||
script.onload();
|
||||
}
|
||||
};
|
||||
|
||||
// 将脚本添加到head中,开始加载
|
||||
document.head.appendChild(script);
|
||||
})
|
||||
}
|
||||
|
||||
async function loadScripts(scripts) {
|
||||
await (async function loadNextScript(i) {
|
||||
if (i < scripts.length) {
|
||||
await loadScript(scripts[i]);
|
||||
await loadNextScript(i + 1)
|
||||
}
|
||||
})(0);
|
||||
|
||||
}
|
||||
let ocr = window?.paddlejs?.ocr??null;
|
||||
let jsFiles = [
|
||||
'/static/tmui4xLibs/lib/ocrLib.js'
|
||||
];
|
||||
const decoderText =async (imgpath:string,callback)=>{
|
||||
if(!window?.paddlejs){
|
||||
await loadScripts(jsFiles)
|
||||
ocr = paddlejs.ocr
|
||||
await ocr.init()
|
||||
}
|
||||
let img = new Image()
|
||||
img.src = imgpath
|
||||
img.onload = async function(){
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = 800;
|
||||
canvas.height = 800
|
||||
const res = await ocr.recognize(img, { canvas });
|
||||
let texts = res.text
|
||||
let bounds = []
|
||||
for(let i=0;i<res.points.length;i++){
|
||||
let item = res.points[i]
|
||||
bounds.push({
|
||||
x:item[0][0],
|
||||
y:item[0][1],
|
||||
width:item[1][0]-item[0][0],
|
||||
height:item[3][1]+item[0][1]
|
||||
})
|
||||
}
|
||||
|
||||
callback(texts,JSON.stringify(bounds))
|
||||
}
|
||||
|
||||
}
|
||||
export async function chooseImageBuilder(callback : (str : string[], source : string[]) => void, langs : string | null) {
|
||||
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success:async (evt : ChooseImageSuccess)=> {
|
||||
uni.showLoading({title:'...',mask:true})
|
||||
if (evt.tempFilePaths.length > 0) {
|
||||
let imgpath = evt.tempFilePaths[0] as string;
|
||||
try {
|
||||
await decoderText(imgpath,callback)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
}
|
||||
uni.hideLoading()
|
||||
},
|
||||
fail() {
|
||||
console.warn("图片选择失败")
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
export async function downloadUrlImageBuilder(url : string, callback : (str : string[], source : string[]) => void, langs : string | null) {
|
||||
uni.showLoading({title:'...',mask:true})
|
||||
try {
|
||||
await decoderText(url,callback)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
callback([] as string[],[] as string[])
|
||||
}
|
||||
uni.hideLoading()
|
||||
}
|
||||
export async function localFilePathImageBuilder(pathfile : string, callback : (str : string[], source : string[]) => void, langs : string | null) {
|
||||
downloadUrlImageBuilder(pathfile,callback)
|
||||
}
|
||||
Reference in New Issue
Block a user