mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-04 17:46:13 +00:00
WIP: add JNI wrappers
This commit is contained in:
@@ -14,7 +14,9 @@ import boofcv.android.ConvertBitmap
|
||||
import boofcv.struct.image.GrayU8
|
||||
import chat.simplex.common.R
|
||||
import chat.simplex.common.views.helpers.errorBitmap
|
||||
import chat.simplex.common.views.helpers.generateNewFileName
|
||||
import chat.simplex.common.views.helpers.getFileName
|
||||
import chat.simplex.common.views.helpers.removeFile
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.InputStream
|
||||
import java.net.URI
|
||||
@@ -35,16 +37,14 @@ actual fun base64ToBitmap(base64ImageString: String): ImageBitmap {
|
||||
}
|
||||
|
||||
actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String {
|
||||
var img = image
|
||||
var str = compressImageStr(img)
|
||||
while (str.length > maxDataSize) {
|
||||
val ratio = sqrt(str.length.toDouble() / maxDataSize.toDouble())
|
||||
val clippedRatio = min(ratio, 2.0)
|
||||
val width = (img.width.toDouble() / clippedRatio).toInt()
|
||||
val height = img.height * width / img.width
|
||||
img = Bitmap.createScaledBitmap(img.asAndroidBitmap(), width, height, true).asImageBitmap()
|
||||
str = compressImageStr(img)
|
||||
}
|
||||
val tmpFileName = generateNewFileName("IMG", "png", tmpDir)
|
||||
val tmpFile = File(tmpDir, tmpFileName)
|
||||
val output = FileOutputStream(tmpFile)
|
||||
compressImageData(image, true).writeTo(output)
|
||||
output.flush()
|
||||
output.close()
|
||||
var str = chatResizeImageToStrSize(tmpFileName, maxDataSize.toInt())
|
||||
removeFile(tmpFileName)
|
||||
return str
|
||||
}
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ extern char *chat_write_file(chat_ctrl ctrl, const char *path, char *ptr, int le
|
||||
extern char *chat_read_file(const char *path, const char *key, const char *nonce);
|
||||
extern char *chat_encrypt_file(chat_ctrl ctrl, const char *from_path, const char *to_path);
|
||||
extern char *chat_decrypt_file(const char *from_path, const char *key, const char *nonce, const char *to_path);
|
||||
extern char *chat_resize_image_to_str_size(const char *from_path, int max_size);
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
Java_chat_simplex_common_platform_CoreKt_chatMigrateInit(JNIEnv *env, __unused jclass clazz, jstring dbPath, jstring dbKey, jstring confirm) {
|
||||
@@ -244,3 +245,11 @@ Java_chat_simplex_common_platform_CoreKt_chatDecryptFile(JNIEnv *env, jclass cla
|
||||
(*env)->ReleaseStringUTFChars(env, to_path, _to_path);
|
||||
return res;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jint max_size) {
|
||||
const char *_from_path = encode_to_utf8_chars(env, from_path);
|
||||
jstring res = decode_to_utf8_string(env, chat_resize_image_to_str_size(_from_path, max_size));
|
||||
(*env)->ReleaseStringUTFChars(env, from_path, _from_path);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ extern char *chat_write_file(chat_ctrl ctrl, const char *path, char *ptr, int le
|
||||
extern char *chat_read_file(const char *path, const char *key, const char *nonce);
|
||||
extern char *chat_encrypt_file(chat_ctrl ctrl, const char *from_path, const char *to_path);
|
||||
extern char *chat_decrypt_file(const char *from_path, const char *key, const char *nonce, const char *to_path);
|
||||
extern char *chat_resize_image_to_str_size(const char *from_path, int max_size);
|
||||
|
||||
// As a reference: https://stackoverflow.com/a/60002045
|
||||
jstring decode_to_utf8_string(JNIEnv *env, char *string) {
|
||||
@@ -254,3 +255,11 @@ Java_chat_simplex_common_platform_CoreKt_chatDecryptFile(JNIEnv *env, jclass cla
|
||||
(*env)->ReleaseStringUTFChars(env, to_path, _to_path);
|
||||
return res;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jint max_size) {
|
||||
const char *_from_path = encode_to_utf8_chars(env, from_path);
|
||||
jstring res = decode_to_utf8_string(env, chat_resize_image_to_str_size(_from_path, max_size));
|
||||
(*env)->ReleaseStringUTFChars(env, from_path, _from_path);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ external fun chatWriteFile(ctrl: ChatCtrl, path: String, buffer: ByteBuffer): St
|
||||
external fun chatReadFile(path: String, key: String, nonce: String): Array<Any>
|
||||
external fun chatEncryptFile(ctrl: ChatCtrl, fromPath: String, toPath: String): String
|
||||
external fun chatDecryptFile(fromPath: String, key: String, nonce: String, toPath: String): String
|
||||
external fun chatResizeImageToStrSize(fromPath: String, maxSize: Int): String
|
||||
|
||||
val chatModel: ChatModel
|
||||
get() = chatController.chatModel
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.io.InputStream
|
||||
import java.net.URI
|
||||
|
||||
expect fun base64ToBitmap(base64ImageString: String): ImageBitmap
|
||||
// XXX: Not a part of platform services anymore?
|
||||
expect fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String
|
||||
expect fun resizeImageToDataSize(image: ImageBitmap, usePng: Boolean, maxDataSize: Long): ByteArrayOutputStream
|
||||
expect fun cropToSquare(image: ImageBitmap): ImageBitmap
|
||||
|
||||
@@ -3,6 +3,8 @@ package chat.simplex.common.platform
|
||||
import androidx.compose.ui.graphics.*
|
||||
import boofcv.io.image.ConvertBufferedImage
|
||||
import boofcv.struct.image.GrayU8
|
||||
import chat.simplex.common.views.helpers.generateNewFileName
|
||||
import chat.simplex.common.views.helpers.removeFile
|
||||
import chat.simplex.res.MR
|
||||
import org.jetbrains.skia.Image
|
||||
import java.awt.RenderingHints
|
||||
@@ -32,18 +34,17 @@ actual fun base64ToBitmap(base64ImageString: String): ImageBitmap {
|
||||
}
|
||||
|
||||
actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String {
|
||||
var img = image
|
||||
var str = compressImageStr(img)
|
||||
while (str.length > maxDataSize) {
|
||||
val ratio = sqrt(str.length.toDouble() / maxDataSize.toDouble())
|
||||
val clippedRatio = kotlin.math.min(ratio, 2.0)
|
||||
val width = (img.width.toDouble() / clippedRatio).toInt()
|
||||
val height = img.height * width / img.width
|
||||
img = img.scale(width, height)
|
||||
str = compressImageStr(img)
|
||||
}
|
||||
val tmpFileName = generateNewFileName("IMG", "png", tmpDir)
|
||||
val tmpFile = File(tmpDir, tmpFileName)
|
||||
val output = FileOutputStream(tmpFile)
|
||||
compressImageData(image, true).writeTo(output)
|
||||
output.flush()
|
||||
output.close()
|
||||
var str = chatResizeImageToStrSize(tmpFileName, maxDataSize.toInt())
|
||||
removeFile(tmpFileName)
|
||||
return str
|
||||
}
|
||||
|
||||
actual fun resizeImageToDataSize(image: ImageBitmap, usePng: Boolean, maxDataSize: Long): ByteArrayOutputStream {
|
||||
var img = image
|
||||
var stream = compressImageData(img, usePng)
|
||||
|
||||
Reference in New Issue
Block a user