|
| 1 | +package ai.quickpose.reactnative |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.os.Handler |
| 6 | +import android.os.HandlerThread |
| 7 | +import android.view.PixelCopy |
| 8 | +import android.view.SurfaceView |
| 9 | +import android.view.View |
| 10 | +import android.view.ViewGroup |
| 11 | +import androidx.core.content.FileProvider |
| 12 | +import com.facebook.react.bridge.Promise |
| 13 | +import com.facebook.react.bridge.ReactApplicationContext |
| 14 | +import com.facebook.react.bridge.ReactContextBaseJavaModule |
| 15 | +import com.facebook.react.bridge.ReactMethod |
| 16 | +import com.facebook.react.uimanager.UIManagerHelper |
| 17 | +import java.io.File |
| 18 | +import java.io.FileOutputStream |
| 19 | + |
| 20 | +class QuickPoseCaptureModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) { |
| 21 | + |
| 22 | + override fun getName(): String = "QuickPoseCaptureModule" |
| 23 | + |
| 24 | + @ReactMethod |
| 25 | + fun captureFrame(viewTag: Int, promise: Promise) { |
| 26 | + captureToFile(viewTag) { result -> |
| 27 | + result.fold( |
| 28 | + onSuccess = { file -> promise.resolve("file://${file.absolutePath}") }, |
| 29 | + onFailure = { err -> promise.reject("capture_failed", err) } |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @ReactMethod |
| 35 | + fun shareFrame(viewTag: Int, title: String?, promise: Promise) { |
| 36 | + captureToFile(viewTag) { result -> |
| 37 | + result.fold( |
| 38 | + onSuccess = { file -> |
| 39 | + try { |
| 40 | + val ctx = reactApplicationContext |
| 41 | + val authority = "${ctx.packageName}.quickpose.fileprovider" |
| 42 | + val uri = FileProvider.getUriForFile(ctx, authority, file) |
| 43 | + val intent = Intent(Intent.ACTION_SEND).apply { |
| 44 | + type = "image/png" |
| 45 | + putExtra(Intent.EXTRA_STREAM, uri) |
| 46 | + if (!title.isNullOrEmpty()) putExtra(Intent.EXTRA_SUBJECT, title) |
| 47 | + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 48 | + } |
| 49 | + val chooser = Intent.createChooser(intent, title ?: "Share").apply { |
| 50 | + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 51 | + } |
| 52 | + ctx.startActivity(chooser) |
| 53 | + promise.resolve(null) |
| 54 | + } catch (e: Exception) { |
| 55 | + promise.reject("share_failed", e) |
| 56 | + } |
| 57 | + }, |
| 58 | + onFailure = { err -> promise.reject("capture_failed", err) } |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun captureToFile(viewTag: Int, onDone: (Result<File>) -> Unit) { |
| 64 | + val ctx = reactApplicationContext |
| 65 | + ctx.runOnUiQueueThread { |
| 66 | + try { |
| 67 | + val view = UIManagerHelper.getUIManager(ctx, viewTag)?.resolveView(viewTag) |
| 68 | + ?: return@runOnUiQueueThread onDone(Result.failure(IllegalStateException("Could not resolve view $viewTag"))) |
| 69 | + val surfaceView = findSurfaceView(view) |
| 70 | + ?: return@runOnUiQueueThread onDone(Result.failure(IllegalStateException("No SurfaceView under QuickPoseView"))) |
| 71 | + val width = surfaceView.width |
| 72 | + val height = surfaceView.height |
| 73 | + if (width == 0 || height == 0) { |
| 74 | + return@runOnUiQueueThread onDone(Result.failure(IllegalStateException("Surface has zero size"))) |
| 75 | + } |
| 76 | + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) |
| 77 | + val thread = HandlerThread("QuickPoseCapture") |
| 78 | + thread.start() |
| 79 | + val handler = Handler(thread.looper) |
| 80 | + PixelCopy.request(surfaceView, bitmap, { copyResult -> |
| 81 | + try { |
| 82 | + if (copyResult != PixelCopy.SUCCESS) { |
| 83 | + onDone(Result.failure(IllegalStateException("PixelCopy failed with $copyResult"))) |
| 84 | + return@request |
| 85 | + } |
| 86 | + val file = File(ctx.cacheDir, "quickpose_${System.currentTimeMillis()}.png") |
| 87 | + FileOutputStream(file).use { out -> |
| 88 | + bitmap.compress(Bitmap.CompressFormat.PNG, 100, out) |
| 89 | + } |
| 90 | + onDone(Result.success(file)) |
| 91 | + } catch (e: Exception) { |
| 92 | + onDone(Result.failure(e)) |
| 93 | + } finally { |
| 94 | + bitmap.recycle() |
| 95 | + thread.quitSafely() |
| 96 | + } |
| 97 | + }, handler) |
| 98 | + } catch (e: Exception) { |
| 99 | + onDone(Result.failure(e)) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun findSurfaceView(root: View): SurfaceView? { |
| 105 | + if (root is SurfaceView) return root |
| 106 | + if (root is ViewGroup) { |
| 107 | + for (i in 0 until root.childCount) { |
| 108 | + val hit = findSurfaceView(root.getChildAt(i)) |
| 109 | + if (hit != null) return hit |
| 110 | + } |
| 111 | + } |
| 112 | + return null |
| 113 | + } |
| 114 | +} |
0 commit comments