forked from simplepeng/wechat-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.kt
107 lines (84 loc) · 3.52 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.tencent.qbar.sample
import android.graphics.Point
import android.graphics.Rect
import android.hardware.Camera
import android.os.Bundle
import android.view.SurfaceHolder
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.tencent.qbar.QbarNative
import com.tencent.qbar.WechatScanner
import kotlinx.android.synthetic.main.activity_main.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.nio.charset.Charset
@Suppress(names = ["DEPRECATION"])
class MainActivity : AppCompatActivity(), SurfaceHolder.Callback, Camera.PreviewCallback, Camera.AutoFocusCallback {
private lateinit var wechatScanner: WechatScanner
private lateinit var camera: Camera
private var isProcessing: Boolean = false
private var isScanFinish: Boolean = false
private val logger: Logger = LoggerFactory.getLogger(this.javaClass)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onClickInit(view: View) {
wechatScanner = WechatScanner()
wechatScanner.releaseAssert(view.context)
wechatScanner.init(view.context)
wechatScanner.setReader()
textView.text = wechatScanner.version()
}
fun onClickOpen(view: View) {
surfaceView.holder.addCallback(this)
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK)
camera.setPreviewDisplay(surfaceView.holder)
camera.setDisplayOrientation(90)
val parameters: Camera.Parameters = camera.parameters
parameters.focusMode = Camera.Parameters.FLASH_MODE_AUTO
camera.parameters = parameters
camera.setPreviewCallback(this)
camera.startPreview()
}
fun onClickFouce(view: View) {
camera.autoFocus(this)
}
fun onClickReset(view: View) {
isScanFinish = false
textView.text = ""
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
}
override fun surfaceCreated(holder: SurfaceHolder) {
}
override fun onPreviewFrame(data: ByteArray, camera: Camera) {
if (isScanFinish || isProcessing)
return
isProcessing = true
val startTimestamp: Long = System.currentTimeMillis()
val scanResultList: List<QbarNative.QBarResultJNI> = wechatScanner.onPreviewFrame(
data = data,
size = Point(camera.parameters.previewSize.width, camera.parameters.previewSize.height),
crop = Rect(373, 36, 1163, 826),
rotation = 90
)
if (scanResultList.isNotEmpty()) {
isScanFinish = true
scanResultList.forEach { qBarResultJNI: QbarNative.QBarResultJNI -> logger.info("LOG:MainActivity:onPreviewFrame typeName={} charset={} data={}", qBarResultJNI.typeName, qBarResultJNI.charset, String(qBarResultJNI.data, Charset.forName(qBarResultJNI.charset))) }
textView.post { textView.text = scanResultList.first().let { String(it.data, Charset.forName(it.charset)) } }
logger.info("LOG:MainActivity:onPreviewFrame scan cost: {}ms", System.currentTimeMillis() - startTimestamp)
}
isProcessing = false
}
override fun onAutoFocus(success: Boolean, camera: Camera?) {
logger.info("LOG:MainActivity:onAutoFocus success={}", success)
}
override fun onDestroy() {
super.onDestroy()
camera.release()
wechatScanner.release()
}
}