react-native-face-match-sdk 0.0.0-stage → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FaceMatch.podspec +55 -0
- package/LICENSE +21 -0
- package/README.md +190 -2
- package/android/build.gradle +59 -0
- package/android/consumer-rules.pro +5 -0
- package/android/src/main/AndroidManifest.xml +11 -0
- package/android/src/main/java/com/facematch/sdk/AntiSpoofHelper.kt +216 -0
- package/android/src/main/java/com/facematch/sdk/BitmapUtils.kt +138 -0
- package/android/src/main/java/com/facematch/sdk/CameraFrameView.kt +75 -0
- package/android/src/main/java/com/facematch/sdk/FaceAlignmentHelper.kt +156 -0
- package/android/src/main/java/com/facematch/sdk/FaceCameraView.kt +628 -0
- package/android/src/main/java/com/facematch/sdk/FaceCameraViewManager.kt +79 -0
- package/android/src/main/java/com/facematch/sdk/FaceCandidateFilter.kt +65 -0
- package/android/src/main/java/com/facematch/sdk/FaceDetectionHelper.kt +225 -0
- package/android/src/main/java/com/facematch/sdk/FaceEmbeddingHelper.kt +242 -0
- package/android/src/main/java/com/facematch/sdk/FaceFrameAnalyzer.kt +228 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatchModule.kt +249 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatchPackage.kt +14 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatchRuntime.kt +138 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatcherHelper.kt +92 -0
- package/android/src/main/java/com/facematch/sdk/FaceOverlayView.kt +236 -0
- package/android/src/main/java/com/facematch/sdk/LivenessDetector.kt +483 -0
- package/android/src/main/java/com/facematch/sdk/LivenessPrompts.kt +137 -0
- package/android/src/main/java/com/facematch/sdk/LivenessVoice.kt +270 -0
- package/android/src/main/java/com/facematch/sdk/PrivateCaptureStore.kt +50 -0
- package/ios/Sources/AntiSpoofHelper.swift +239 -0
- package/ios/Sources/FaceAlignmentHelper.swift +169 -0
- package/ios/Sources/FaceCameraView.swift +1161 -0
- package/ios/Sources/FaceCameraViewManager.m +28 -0
- package/ios/Sources/FaceCameraViewManager.swift +57 -0
- package/ios/Sources/FaceCandidateFilter.swift +62 -0
- package/ios/Sources/FaceDetectionHelper.swift +283 -0
- package/ios/Sources/FaceEmbeddingHelper.swift +188 -0
- package/ios/Sources/FaceFrameAnalyzer.swift +179 -0
- package/ios/Sources/FaceFrameResult.swift +96 -0
- package/ios/Sources/FaceMatchBundle.swift +26 -0
- package/ios/Sources/FaceMatchEngine.swift +136 -0
- package/ios/Sources/FaceMatchError.swift +40 -0
- package/ios/Sources/FaceMatchModule.m +55 -0
- package/ios/Sources/FaceMatchModule.swift +288 -0
- package/ios/Sources/FaceMatcherHelper.swift +107 -0
- package/ios/Sources/FaceOverlayView.swift +285 -0
- package/ios/Sources/ImageUtils.swift +322 -0
- package/ios/Sources/LivenessDetector.swift +505 -0
- package/ios/Sources/LivenessPrompts.swift +121 -0
- package/ios/Sources/LivenessVoice.swift +229 -0
- package/ios/Sources/OnnxRuntime/ORTShim.h +91 -0
- package/ios/Sources/OnnxRuntime/ORTShim.mm +260 -0
- package/ios/Sources/PrivateCaptureStore.swift +71 -0
- package/ios/Sources/VisionFaceFallback.swift +95 -0
- package/ios/onnxruntime-c.podspec +47 -0
- package/models/anti_spoof_minifasnet_v2.onnx +0 -0
- package/models/w600k_mbf.onnx +0 -0
- package/package.json +47 -4
- package/react-native.config.js +16 -0
- package/src/FaceCameraView.tsx +75 -0
- package/src/FaceMatch.ts +66 -0
- package/src/index.ts +4 -0
- package/src/types.ts +87 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Bitmap
|
|
5
|
+
import android.graphics.BitmapFactory
|
|
6
|
+
import android.graphics.Matrix
|
|
7
|
+
import android.net.Uri
|
|
8
|
+
import androidx.exifinterface.media.ExifInterface
|
|
9
|
+
import java.io.File
|
|
10
|
+
import java.io.FileInputStream
|
|
11
|
+
import java.io.InputStream
|
|
12
|
+
|
|
13
|
+
object BitmapUtils {
|
|
14
|
+
private const val MAX_SOURCE_BYTES = 64L * 1024L * 1024L
|
|
15
|
+
private const val MAX_DECODED_DIMENSION = 4096
|
|
16
|
+
private const val MAX_DECODED_PIXELS = 8L * 1024L * 1024L
|
|
17
|
+
|
|
18
|
+
fun decode(context: Context, uriString: String): Bitmap {
|
|
19
|
+
require(uriString.isNotBlank()) { "Image URI must not be empty" }
|
|
20
|
+
val uri = validateUri(context, Uri.parse(uriString))
|
|
21
|
+
validateKnownSize(context, uri)
|
|
22
|
+
|
|
23
|
+
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
|
24
|
+
open(context, uri).use { BitmapFactory.decodeStream(it, null, bounds) }
|
|
25
|
+
require(bounds.outWidth > 0 && bounds.outHeight > 0) {
|
|
26
|
+
"Unsupported or unreadable image"
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var sampleSize = 1
|
|
30
|
+
while (
|
|
31
|
+
bounds.outWidth / sampleSize > MAX_DECODED_DIMENSION ||
|
|
32
|
+
bounds.outHeight / sampleSize > MAX_DECODED_DIMENSION ||
|
|
33
|
+
(bounds.outWidth.toLong() / sampleSize) * (bounds.outHeight.toLong() / sampleSize) > MAX_DECODED_PIXELS
|
|
34
|
+
) {
|
|
35
|
+
sampleSize *= 2
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
val options = BitmapFactory.Options().apply {
|
|
39
|
+
inSampleSize = sampleSize
|
|
40
|
+
inPreferredConfig = Bitmap.Config.ARGB_8888
|
|
41
|
+
}
|
|
42
|
+
val bitmap = open(context, uri).use { stream ->
|
|
43
|
+
BitmapFactory.decodeStream(stream, null, options)
|
|
44
|
+
?: throw IllegalArgumentException("Unsupported or unreadable image")
|
|
45
|
+
}
|
|
46
|
+
val orientation = runCatching {
|
|
47
|
+
open(context, uri).use { stream ->
|
|
48
|
+
ExifInterface(stream).getAttributeInt(
|
|
49
|
+
ExifInterface.TAG_ORIENTATION,
|
|
50
|
+
ExifInterface.ORIENTATION_NORMAL
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}.getOrDefault(ExifInterface.ORIENTATION_NORMAL)
|
|
54
|
+
return orient(bitmap, orientation)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private fun validateUri(context: Context, uri: Uri): Uri {
|
|
58
|
+
return when (uri.scheme?.lowercase()) {
|
|
59
|
+
"content" -> {
|
|
60
|
+
require(!uri.authority.isNullOrBlank()) { "Content URI must include an authority" }
|
|
61
|
+
uri
|
|
62
|
+
}
|
|
63
|
+
"android.resource" -> uri
|
|
64
|
+
"file" -> {
|
|
65
|
+
val path = requireNotNull(uri.path) { "File URI must include a path" }
|
|
66
|
+
val target = File(path).canonicalFile
|
|
67
|
+
val allowedRoots = listOf(
|
|
68
|
+
context.filesDir,
|
|
69
|
+
context.cacheDir,
|
|
70
|
+
context.noBackupFilesDir,
|
|
71
|
+
context.codeCacheDir
|
|
72
|
+
).map { it.canonicalFile }
|
|
73
|
+
require(allowedRoots.any { root ->
|
|
74
|
+
target == root || target.path.startsWith(root.path + File.separator)
|
|
75
|
+
}) { "File URI must point to app-private storage" }
|
|
76
|
+
Uri.fromFile(target)
|
|
77
|
+
}
|
|
78
|
+
else -> throw IllegalArgumentException(
|
|
79
|
+
"Only content://, android.resource://, and app-private file:// images are supported"
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private fun validateKnownSize(context: Context, uri: Uri) {
|
|
85
|
+
val length = when (uri.scheme?.lowercase()) {
|
|
86
|
+
"file" -> File(requireNotNull(uri.path)).length()
|
|
87
|
+
"content", "android.resource" -> runCatching {
|
|
88
|
+
context.contentResolver.openAssetFileDescriptor(uri, "r")?.use { it.length }
|
|
89
|
+
}.getOrNull() ?: -1L
|
|
90
|
+
else -> -1L
|
|
91
|
+
}
|
|
92
|
+
require(length <= 0L || length <= MAX_SOURCE_BYTES) { "Image file is too large" }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private fun open(context: Context, uri: Uri): InputStream {
|
|
96
|
+
return when (uri.scheme?.lowercase()) {
|
|
97
|
+
"content", "android.resource" -> context.contentResolver.openInputStream(uri)
|
|
98
|
+
"file" -> FileInputStream(File(requireNotNull(uri.path)))
|
|
99
|
+
null, "" -> FileInputStream(File(uri.toString()))
|
|
100
|
+
else -> throw IllegalArgumentException("Only local content:// and file:// images are supported")
|
|
101
|
+
} ?: throw IllegalArgumentException("Unable to open image: $uri")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private fun orient(source: Bitmap, orientation: Int): Bitmap {
|
|
105
|
+
val matrix = Matrix()
|
|
106
|
+
when (orientation) {
|
|
107
|
+
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1f, 1f)
|
|
108
|
+
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180f)
|
|
109
|
+
ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.setScale(1f, -1f)
|
|
110
|
+
ExifInterface.ORIENTATION_TRANSPOSE -> {
|
|
111
|
+
matrix.setRotate(90f)
|
|
112
|
+
matrix.postScale(-1f, 1f)
|
|
113
|
+
}
|
|
114
|
+
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.setRotate(90f)
|
|
115
|
+
ExifInterface.ORIENTATION_TRANSVERSE -> {
|
|
116
|
+
matrix.setRotate(-90f)
|
|
117
|
+
matrix.postScale(-1f, 1f)
|
|
118
|
+
}
|
|
119
|
+
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.setRotate(-90f)
|
|
120
|
+
else -> return source
|
|
121
|
+
}
|
|
122
|
+
return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
|
|
123
|
+
.also { if (it !== source) source.recycle() }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fun rotateDegrees(source: Bitmap, degrees: Int): Bitmap {
|
|
127
|
+
if (degrees % 360 == 0) return source
|
|
128
|
+
val matrix = Matrix().apply { postRotate(degrees.toFloat()) }
|
|
129
|
+
return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
|
|
130
|
+
.also { if (it !== source) source.recycle() }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fun mirrorHorizontal(source: Bitmap): Bitmap {
|
|
134
|
+
val matrix = Matrix().apply { preScale(-1f, 1f) }
|
|
135
|
+
return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
|
|
136
|
+
.also { if (it !== source) source.recycle() }
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Bitmap
|
|
5
|
+
import android.graphics.Canvas
|
|
6
|
+
import android.graphics.Paint
|
|
7
|
+
import android.graphics.Rect
|
|
8
|
+
import android.graphics.RectF
|
|
9
|
+
import android.os.Looper
|
|
10
|
+
import android.view.View
|
|
11
|
+
import kotlin.math.roundToInt
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Displays the upright frames used by face analysis.
|
|
15
|
+
*
|
|
16
|
+
* Some Android vendors fail to compose CameraX's PreviewView when it is nested
|
|
17
|
+
* in a React Native scrolling/clipping container. Rendering the already-local
|
|
18
|
+
* analysis frame keeps the preview visible without adding another camera feed.
|
|
19
|
+
*/
|
|
20
|
+
class CameraFrameView(context: Context) : View(context) {
|
|
21
|
+
private val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
|
|
22
|
+
private var frame: Bitmap? = null
|
|
23
|
+
|
|
24
|
+
fun update(bitmap: Bitmap) {
|
|
25
|
+
runOnUiThread {
|
|
26
|
+
val previous = frame
|
|
27
|
+
frame = bitmap
|
|
28
|
+
invalidate()
|
|
29
|
+
if (previous !== bitmap && previous?.isRecycled == false) previous.recycle()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fun clear() {
|
|
34
|
+
runOnUiThread {
|
|
35
|
+
val previous = frame
|
|
36
|
+
frame = null
|
|
37
|
+
invalidate()
|
|
38
|
+
if (previous?.isRecycled == false) previous.recycle()
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
override fun onDraw(canvas: Canvas) {
|
|
43
|
+
super.onDraw(canvas)
|
|
44
|
+
val bitmap = frame ?: return
|
|
45
|
+
if (bitmap.isRecycled || width <= 0 || height <= 0) return
|
|
46
|
+
|
|
47
|
+
val source = centerCrop(bitmap.width, bitmap.height, width, height)
|
|
48
|
+
val destination = RectF(0f, 0f, width.toFloat(), height.toFloat())
|
|
49
|
+
|
|
50
|
+
// Match the usual front-camera preview rather than showing an
|
|
51
|
+
// unexpectedly reversed image to the person completing liveness.
|
|
52
|
+
canvas.save()
|
|
53
|
+
canvas.scale(-1f, 1f, width / 2f, height / 2f)
|
|
54
|
+
canvas.drawBitmap(bitmap, source, destination, paint)
|
|
55
|
+
canvas.restore()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private fun centerCrop(sourceWidth: Int, sourceHeight: Int, targetWidth: Int, targetHeight: Int): Rect {
|
|
59
|
+
val sourceAspect = sourceWidth.toFloat() / sourceHeight
|
|
60
|
+
val targetAspect = targetWidth.toFloat() / targetHeight
|
|
61
|
+
return if (sourceAspect > targetAspect) {
|
|
62
|
+
val cropWidth = (sourceHeight * targetAspect).roundToInt().coerceAtMost(sourceWidth)
|
|
63
|
+
val left = (sourceWidth - cropWidth) / 2
|
|
64
|
+
Rect(left, 0, left + cropWidth, sourceHeight)
|
|
65
|
+
} else {
|
|
66
|
+
val cropHeight = (sourceWidth / targetAspect).roundToInt().coerceAtMost(sourceHeight)
|
|
67
|
+
val top = (sourceHeight - cropHeight) / 2
|
|
68
|
+
Rect(0, top, sourceWidth, top + cropHeight)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private fun runOnUiThread(action: () -> Unit) {
|
|
73
|
+
if (Looper.myLooper() == Looper.getMainLooper()) action() else if (!post { action() }) action()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
import android.graphics.Bitmap
|
|
4
|
+
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.Matrix
|
|
6
|
+
import android.graphics.Paint
|
|
7
|
+
import kotlin.math.sqrt
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Aligns a detected face to the InsightFace standard 112×112 crop using a
|
|
11
|
+
* 5-point similarity transform (Umeyama algorithm).
|
|
12
|
+
*
|
|
13
|
+
* The reference landmarks match those used by the Python backend's buffalo_sc
|
|
14
|
+
* model, so embeddings produced here are directly comparable to server-side ones.
|
|
15
|
+
*
|
|
16
|
+
* Landmark order: left_eye, right_eye, nose_tip, left_mouth_corner, right_mouth_corner
|
|
17
|
+
*/
|
|
18
|
+
object FaceAlignmentHelper {
|
|
19
|
+
|
|
20
|
+
const val FACE_SIZE = 112
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* InsightFace reference 5-point positions for a 112×112 aligned crop.
|
|
24
|
+
* Stored as [x0,y0, x1,y1, …] flat array.
|
|
25
|
+
*/
|
|
26
|
+
private val REFERENCE_LANDMARKS = floatArrayOf(
|
|
27
|
+
38.2946f, 51.6963f, // left eye
|
|
28
|
+
73.5318f, 51.5014f, // right eye
|
|
29
|
+
56.0252f, 71.7366f, // nose tip
|
|
30
|
+
41.5493f, 92.3655f, // left mouth corner
|
|
31
|
+
70.7299f, 92.2041f // right mouth corner
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Tightly crops the face region from [bitmap] using [face]'s bounding box
|
|
36
|
+
* plus a [marginFraction] margin on each side (default 30%).
|
|
37
|
+
*
|
|
38
|
+
* Why crop first?
|
|
39
|
+
* Giving the Umeyama transform a tight face region rather than the whole
|
|
40
|
+
* image avoids floating-point precision loss and guarantees the 112×112
|
|
41
|
+
* output is filled by the face, not by surrounding background.
|
|
42
|
+
*
|
|
43
|
+
* @return Pair of (cropped bitmap, landmarks translated into cropped coords)
|
|
44
|
+
*/
|
|
45
|
+
fun cropFaceRegion(
|
|
46
|
+
bitmap: Bitmap,
|
|
47
|
+
face: DetectedFace,
|
|
48
|
+
marginFraction: Float = 0.30f
|
|
49
|
+
): Pair<Bitmap, FloatArray> {
|
|
50
|
+
val bw = face.boundingBox.width()
|
|
51
|
+
val bh = face.boundingBox.height()
|
|
52
|
+
val mx = bw * marginFraction
|
|
53
|
+
val my = bh * marginFraction
|
|
54
|
+
|
|
55
|
+
val x0 = (face.boundingBox.left - mx).coerceAtLeast(0f).toInt()
|
|
56
|
+
val y0 = (face.boundingBox.top - my).coerceAtLeast(0f).toInt()
|
|
57
|
+
val x1 = (face.boundingBox.right + mx).coerceAtMost(bitmap.width.toFloat()).toInt()
|
|
58
|
+
val y1 = (face.boundingBox.bottom + my).coerceAtMost(bitmap.height.toFloat()).toInt()
|
|
59
|
+
|
|
60
|
+
val cropW = (x1 - x0).coerceAtLeast(1)
|
|
61
|
+
val cropH = (y1 - y0).coerceAtLeast(1)
|
|
62
|
+
|
|
63
|
+
val cropped = Bitmap.createBitmap(bitmap, x0, y0, cropW, cropH)
|
|
64
|
+
|
|
65
|
+
// Translate landmark coordinates into the cropped image's coordinate space
|
|
66
|
+
val adjustedLandmarks = face.landmarks.copyOf()
|
|
67
|
+
for (i in adjustedLandmarks.indices) {
|
|
68
|
+
adjustedLandmarks[i] -= if (i % 2 == 0) x0.toFloat() else y0.toFloat()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return Pair(cropped, adjustedLandmarks)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Warp [bitmap] so that [srcLandmarks] (flat x,y pairs in bitmap pixel space)
|
|
76
|
+
* align to the reference 112×112 positions.
|
|
77
|
+
*
|
|
78
|
+
* @param bitmap Full-resolution source image (BGR/ARGB does not matter here;
|
|
79
|
+
* colour conversion to RGB is done in FaceEmbeddingHelper).
|
|
80
|
+
* @param srcLandmarks Flat float array [lx,ly, rx,ry, nx,ny, lmx,lmy, rmx,rmy]
|
|
81
|
+
* in bitmap pixel coordinates.
|
|
82
|
+
* @return ARGB_8888 bitmap of size 112×112.
|
|
83
|
+
*/
|
|
84
|
+
fun alignFace(bitmap: Bitmap, srcLandmarks: FloatArray): Bitmap {
|
|
85
|
+
require(srcLandmarks.size == 10) { "Need 5 landmarks (10 floats), got ${srcLandmarks.size}" }
|
|
86
|
+
val matrix = estimateSimilarityTransform(srcLandmarks, REFERENCE_LANDMARKS)
|
|
87
|
+
val output = Bitmap.createBitmap(FACE_SIZE, FACE_SIZE, Bitmap.Config.ARGB_8888)
|
|
88
|
+
// Use bilinear filtering for a smoother, higher-quality warped crop
|
|
89
|
+
val paint = Paint().also {
|
|
90
|
+
it.isFilterBitmap = true
|
|
91
|
+
it.isAntiAlias = true
|
|
92
|
+
}
|
|
93
|
+
Canvas(output).drawBitmap(bitmap, matrix, paint)
|
|
94
|
+
return output
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// -----------------------------------------------------------------------
|
|
98
|
+
// Umeyama similarity-transform estimation
|
|
99
|
+
// -----------------------------------------------------------------------
|
|
100
|
+
// For 2D correspondences the optimal similarity transform (rotation, uniform
|
|
101
|
+
// scale, translation — no reflection) has a closed-form solution:
|
|
102
|
+
//
|
|
103
|
+
// a = Σ(sx·dx + sy·dy) / Σ(sx²+sy²)
|
|
104
|
+
// b = Σ(sx·dy − sy·dx) / Σ(sx²+sy²)
|
|
105
|
+
//
|
|
106
|
+
// where (sx,sy) = src point − src centroid, (dx,dy) = dst point − dst centroid.
|
|
107
|
+
//
|
|
108
|
+
// The resulting 2×3 affine matrix is: [ a −b tx ]
|
|
109
|
+
// [ b a ty ]
|
|
110
|
+
//
|
|
111
|
+
// which encodes: scale = √(a²+b²), angle = atan2(b,a)
|
|
112
|
+
// -----------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
private fun estimateSimilarityTransform(src: FloatArray, dst: FloatArray): Matrix {
|
|
115
|
+
val n = src.size / 2
|
|
116
|
+
|
|
117
|
+
var srcMx = 0.0; var srcMy = 0.0
|
|
118
|
+
var dstMx = 0.0; var dstMy = 0.0
|
|
119
|
+
for (i in 0 until n) {
|
|
120
|
+
srcMx += src[2 * i]; srcMy += src[2 * i + 1]
|
|
121
|
+
dstMx += dst[2 * i]; dstMy += dst[2 * i + 1]
|
|
122
|
+
}
|
|
123
|
+
srcMx /= n; srcMy /= n; dstMx /= n; dstMy /= n
|
|
124
|
+
|
|
125
|
+
var a = 0.0; var b = 0.0; var srcVar = 0.0
|
|
126
|
+
for (i in 0 until n) {
|
|
127
|
+
val sx = src[2 * i] - srcMx; val sy = src[2 * i + 1] - srcMy
|
|
128
|
+
val dx = dst[2 * i] - dstMx; val dy = dst[2 * i + 1] - dstMy
|
|
129
|
+
a += sx * dx + sy * dy
|
|
130
|
+
b += sx * dy - sy * dx
|
|
131
|
+
srcVar += sx * sx + sy * sy
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Degenerate case: all source points are identical
|
|
135
|
+
if (srcVar < 1e-10) return Matrix()
|
|
136
|
+
|
|
137
|
+
a /= srcVar; b /= srcVar
|
|
138
|
+
|
|
139
|
+
val tx = dstMx - a * srcMx + b * srcMy
|
|
140
|
+
val ty = dstMy - b * srcMx - a * srcMy
|
|
141
|
+
|
|
142
|
+
// Android Matrix is row-major 3×3. Index mapping:
|
|
143
|
+
// MSCALE_X=0 MSKEW_X=1 MTRANS_X=2
|
|
144
|
+
// MSKEW_Y=3 MSCALE_Y=4 MTRANS_Y=5
|
|
145
|
+
// MPERSP_0=6 MPERSP_1=7 MPERSP_2=8
|
|
146
|
+
return Matrix().also { m ->
|
|
147
|
+
m.setValues(
|
|
148
|
+
floatArrayOf(
|
|
149
|
+
a.toFloat(), (-b).toFloat(), tx.toFloat(),
|
|
150
|
+
b.toFloat(), a.toFloat(), ty.toFloat(),
|
|
151
|
+
0f, 0f, 1f
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|