|
| 1 | +package com.swmansion.rnexecutorch.utils |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.net.Uri |
| 5 | +import android.util.Base64 |
| 6 | +import org.opencv.core.CvType |
| 7 | +import org.opencv.core.Mat |
| 8 | +import org.opencv.imgcodecs.Imgcodecs |
| 9 | +import org.pytorch.executorch.EValue |
| 10 | +import org.pytorch.executorch.Tensor |
| 11 | +import java.io.File |
| 12 | +import java.io.InputStream |
| 13 | +import java.net.URL |
| 14 | +import java.util.UUID |
| 15 | + |
| 16 | + |
| 17 | +class ImageProcessor { |
| 18 | + companion object { |
| 19 | + fun matToEValue(mat: Mat, shape: LongArray): EValue { |
| 20 | + val pixelCount = mat.cols() * mat.rows() |
| 21 | + val floatArray = FloatArray(pixelCount * 3) |
| 22 | + |
| 23 | + for (i in 0 until pixelCount) { |
| 24 | + val row = i / mat.cols() |
| 25 | + val col = i % mat.cols() |
| 26 | + val pixel = mat.get(row, col) |
| 27 | + |
| 28 | + if (mat.type() == CvType.CV_8UC3 || mat.type() == CvType.CV_8UC4) { |
| 29 | + val b = pixel[0] / 255.0f |
| 30 | + val g = pixel[1] / 255.0f |
| 31 | + val r = pixel[2] / 255.0f |
| 32 | + |
| 33 | + floatArray[i] = r.toFloat() |
| 34 | + floatArray[pixelCount + i] = g.toFloat() |
| 35 | + floatArray[2 * pixelCount + i] = b.toFloat() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return EValue.from(Tensor.fromBlob(floatArray, shape)) |
| 40 | + } |
| 41 | + |
| 42 | + fun EValueToMat(array: FloatArray, width: Int, height: Int): Mat { |
| 43 | + val mat = Mat(height, width, CvType.CV_8UC3) |
| 44 | + |
| 45 | + val pixelCount = width * height |
| 46 | + for (i in 0 until pixelCount) { |
| 47 | + val row = i / width |
| 48 | + val col = i % width |
| 49 | + |
| 50 | + val r = (array[i] * 255).toInt().toByte() |
| 51 | + val g = (array[pixelCount + i] * 255).toInt().toByte() |
| 52 | + val b = (array[2 * pixelCount + i] * 255).toInt().toByte() |
| 53 | + |
| 54 | + val color = byteArrayOf(b, g, r) |
| 55 | + mat.put(row, col, color) |
| 56 | + } |
| 57 | + return mat |
| 58 | + } |
| 59 | + |
| 60 | + fun saveToTempFile(context: Context, mat: Mat): String { |
| 61 | + try { |
| 62 | + val uniqueID = UUID.randomUUID().toString() |
| 63 | + val tempFile = File(context.cacheDir, "rn_executorch_$uniqueID.png") |
| 64 | + Imgcodecs.imwrite(tempFile.absolutePath, mat) |
| 65 | + |
| 66 | + return "file://${tempFile.absolutePath}" |
| 67 | + }catch (e: Exception) { |
| 68 | + throw Exception(ETError.FileWriteFailed.toString()) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + fun readImage(source: String): Mat { |
| 73 | + val inputImage: Mat |
| 74 | + |
| 75 | + val uri = Uri.parse(source) |
| 76 | + val scheme = uri.scheme ?: "" |
| 77 | + |
| 78 | + when { |
| 79 | + scheme.equals("data", ignoreCase = true) -> { |
| 80 | + //base64 |
| 81 | + val parts = source.split(",", limit = 2) |
| 82 | + if (parts.size < 2) throw IllegalArgumentException(ETError.InvalidArgument.toString()) |
| 83 | + |
| 84 | + val encodedString = parts[1] |
| 85 | + val data = Base64.decode(encodedString, Base64.DEFAULT) |
| 86 | + |
| 87 | + val encodedData = Mat(1, data.size, CvType.CV_8UC1).apply { |
| 88 | + put(0, 0, data) |
| 89 | + } |
| 90 | + inputImage = Imgcodecs.imdecode(encodedData, Imgcodecs.IMREAD_COLOR) |
| 91 | + } |
| 92 | + scheme.equals("file", ignoreCase = true) -> { |
| 93 | + //device storage |
| 94 | + val path = uri.path |
| 95 | + inputImage = Imgcodecs.imread(path, Imgcodecs.IMREAD_COLOR) |
| 96 | + } |
| 97 | + else -> { |
| 98 | + //external source |
| 99 | + val url = URL(source) |
| 100 | + val connection = url.openConnection() |
| 101 | + connection.connect() |
| 102 | + |
| 103 | + val inputStream: InputStream = connection.getInputStream() |
| 104 | + val data = inputStream.readBytes() |
| 105 | + inputStream.close() |
| 106 | + |
| 107 | + val encodedData = Mat(1, data.size, CvType.CV_8UC1).apply { |
| 108 | + put(0, 0, data) |
| 109 | + } |
| 110 | + inputImage = Imgcodecs.imdecode(encodedData, Imgcodecs.IMREAD_COLOR) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (inputImage.empty()) { |
| 115 | + throw IllegalArgumentException(ETError.InvalidArgument.toString()) |
| 116 | + } |
| 117 | + |
| 118 | + return inputImage |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments