Skip to content

Commit ccbc1d2

Browse files
committed
fix(ocr): 补充ocr功能
1 parent 90ed9bd commit ccbc1d2

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

miniprogram/packageAPI/pages/ar/photo-ocr-detect/behavior.js

+55-8
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,72 @@ export default function getBehavior() {
129129
console.log("anchor add")
130130
})
131131
session.on('updateAnchors', anchors => {
132-
// 手动传入图像的时候用dom画点和框就行
132+
this.data.textContentList = []
133+
console.log(anchors)
134+
this.data.textContentList = this.data.textContentList.concat(anchors.map(anchor => {
135+
let result = {}
136+
result = {
137+
text: anchor.text,
138+
subtext: anchor.subtext,
139+
box: anchor.box,
140+
centerX: anchor.centerX,
141+
centerY: anchor.centerY,
142+
}
143+
if(anchor.box){
144+
let lt = anchor.box[0]
145+
let lr = anchor.box[1]
146+
let rb = anchor.box[2]
147+
let lb = anchor.box[3]
148+
let width = lr.x - lt.x
149+
let height = lb.y - lt.y
150+
let avgX = (lt.x + lr.x + rb.x + lb.x) / 4;
151+
let avgY = (lt.y + lr.y + rb.y + lb.y) / 4;
152+
anchor.centerX = avgX * this.data.faceImgWidth;
153+
anchor.centerY = avgY * this.data.faceImgHeight;
154+
result.origin = {
155+
x: lt.x,
156+
y: lt.y,
157+
}
158+
result.size = {
159+
width: width,
160+
height: height,
161+
}
162+
}
163+
return result
164+
}))
165+
166+
var wholeText = undefined
167+
if(this.data.textContentList.length != 0){
168+
wholeText = this.data.textContentList[0].text
169+
}
170+
133171
this.setData({
134-
textContentList: anchors.map(anchor => ({
135-
text: anchor.text
136-
})),
172+
textContentList: this.data.textContentList,
173+
wholeText: wholeText
137174
})
138175
})
139176
session.on('removeAnchors', anchors => {
140177
console.log("anchor remove")
141178
})
142179

180+
181+
//限制调用帧率
182+
let fps = 30
183+
let fpsInterval = 1000 / fps
184+
let last = Date.now()
185+
143186
// 逐帧渲染
144187
const onFrame = timestamp => {
145-
// let start = Date.now()
146-
const frame = session.getVKFrame(canvas.width, canvas.height)
147-
if (frame) {
188+
let now = Date.now()
189+
const mill = now - last
190+
// 经过了足够的时间
191+
if (mill > fpsInterval) {
192+
last = now - (mill % fpsInterval); //校正当前时间
193+
const frame = session.getVKFrame(canvas.width, canvas.height)
194+
if (frame) {
148195
this.render(frame)
196+
}
149197
}
150-
151198
session.requestAnimationFrame(onFrame)
152199
}
153200
session.requestAnimationFrame(onFrame)

miniprogram/packageAPI/pages/ar/photo-ocr-detect/photo-ocr-detect.wxml

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
</view>
1212

1313

14-
<view wx:if="{{textContentList}}">
15-
<view wx:for="{{textContentList}}">
16-
<text>{{item.text}}</text>
14+
<view wx:if="{{textContentList}}" style="position:relative; height:30%">
15+
<image src="{{faceImgUrl}}" style="position:absolute; left:50%; transform:translate(-50%); width: {{faceImgWidth}}px; height: {{faceImgHeight}}px;" />
16+
<view wx:for="{{textContentList}}" style="position:absolute; top: 0px; margin: 30px; width: {{faceImgWidth}}px; height: {{faceImgHeight}}px;">
17+
<text wx:if="{{item.subtext != undefined && item.centerX != undefined}}" style="position:absolute; top:{{item.centerY}}px; left:{{item.centerX}}px">{{item.subtext}}</text>
18+
<view wx:if="{{item.orign != undefined}}" style="position: absolute; left: {{item.origin.x * faceImgWidth}}px; top: {{item.origin.y * faceImgHeight}}px; width: {{item.size.width * faceImgWidth}}px; height: {{item.size.height * faceImgHeight}}px; border: solid red 2px"></view>
1719
</view>
1820
</view>
19-
<view class="page-body-text tc" style="height:20%">提示:点击选择图片,上传本地照片,然后开始文字检测,将会显示检测到的文字</view>
21+
<view class="page-body-text tc" style="position:relative; height:20%">
22+
<text>提示:点击选择图片,上传本地照片,然后开始文字检测,将会显示检测到的文字</text>
23+
<text style="position:absolute; top:50%; left:0%">{{wholeText}}</text>
24+
</view>
2025
</view>

0 commit comments

Comments
 (0)