From 001c153205bb0294206bca6f4a48e357f5938cab Mon Sep 17 00:00:00 2001 From: Dahyeon Park Date: Wed, 22 Oct 2025 19:58:22 +0900 Subject: [PATCH] add avif encoding --- app/src/main/cpp/libavif_jni.cc | 6 +- .../ble_demo_app_peripheral_2/MainActivity.kt | 102 +++++++++++- .../{ => avif}/AvifCodec.kt | 3 +- .../avif/AvifUtils.kt | 92 +++++++++++ .../ble/CentralManager.kt | 145 ++++++++++++++---- 5 files changed, 309 insertions(+), 39 deletions(-) rename app/src/main/java/com/example/ble_demo_app_peripheral_2/{ => avif}/AvifCodec.kt (98%) create mode 100644 app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifUtils.kt diff --git a/app/src/main/cpp/libavif_jni.cc b/app/src/main/cpp/libavif_jni.cc index 3ab8a68..3a4a1db 100644 --- a/app/src/main/cpp/libavif_jni.cc +++ b/app/src/main/cpp/libavif_jni.cc @@ -11,10 +11,10 @@ #define FUNC(RETURN_TYPE, NAME, ...) \ extern "C" { \ - JNIEXPORT RETURN_TYPE Java_com_example_ble_1demo_1app_1peripheral_12_AvifCodec_##NAME( \ + JNIEXPORT RETURN_TYPE Java_com_example_ble_1demo_1app_1peripheral_12_avif_AvifCodec_##NAME( \ JNIEnv* env, jobject /*thiz*/, ##__VA_ARGS__); \ } \ - JNIEXPORT RETURN_TYPE Java_com_example_ble_1demo_1app_1peripheral_12_AvifCodec_##NAME( \ + JNIEXPORT RETURN_TYPE Java_com_example_ble_1demo_1app_1peripheral_12_avif_AvifCodec_##NAME( \ JNIEnv* env, jobject /*thiz*/, ##__VA_ARGS__) namespace { @@ -91,7 +91,7 @@ jint JNI_OnLoad(JavaVM *vm, void * /*reserved*/) { return -1; } const jclass info_class = - env->FindClass("com/example/ble_demo_app_peripheral_2/AvifCodec$Info"); + env->FindClass("com/example/ble_demo_app_peripheral_2/avif/AvifCodec$Info"); global_info_width = env->GetFieldID(info_class, "width", "I"); global_info_height = env->GetFieldID(info_class, "height", "I"); global_info_depth = env->GetFieldID(info_class, "depth", "I"); diff --git a/app/src/main/java/com/example/ble_demo_app_peripheral_2/MainActivity.kt b/app/src/main/java/com/example/ble_demo_app_peripheral_2/MainActivity.kt index b26fc4f..2495f57 100644 --- a/app/src/main/java/com/example/ble_demo_app_peripheral_2/MainActivity.kt +++ b/app/src/main/java/com/example/ble_demo_app_peripheral_2/MainActivity.kt @@ -6,12 +6,14 @@ import android.content.Context import android.database.Cursor import android.graphics.Bitmap import android.graphics.BitmapFactory +import android.media.MediaScannerConnection import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Environment import android.provider.MediaStore import android.provider.OpenableColumns +import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts @@ -27,6 +29,8 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp +import com.example.ble_demo_app_peripheral_2.avif.AvifCodec +import com.example.ble_demo_app_peripheral_2.avif.encodeAvifFromUri import com.example.ble_demo_app_peripheral_2.ble.CentralManager import com.example.ble_demo_app_peripheral_2.ui.theme.Bledemoappperipheral2Theme import kotlinx.coroutines.MainScope @@ -34,6 +38,7 @@ import java.io.File import java.io.FileOutputStream import java.io.InputStream import java.nio.ByteBuffer +import java.nio.ByteOrder class MainActivity : ComponentActivity() { private val scope = MainScope() @@ -118,7 +123,10 @@ fun SendBLEScreen(cm: CentralManager) { Button(onClick = { imageUri?.let { uri -> context.contentResolver.openInputStream(uri)?.use { ins -> - cm.sendImage(copyToMemory(ins)) + val fileName = queryDisplayName(context, uri) + val encodedImage = encodeAvifFromUri(context, uri!!) + cm.sendByteImage(encodedImage.bytes, encodedImage.fileName) + //cm.sendImage(copyToMemory(ins), fileName) } } }, enabled = imageUri != null) { Text("Send") } @@ -127,7 +135,95 @@ fun SendBLEScreen(cm: CentralManager) { } } +@Composable +fun encodeAVIF(uri: Uri, context: Context) { + // 원본 파일명 기반으로 .avif 이름 만들기 + val originalName = queryDisplayName(context, uri) + var internalFile by remember { mutableStateOf(null) } + var exportFileName by remember { mutableStateOf("output.avif") } + + // API 28 이하에서 WRITE_EXTERNAL_STORAGE 권한 요청 후 저장 실행 + val requestWritePerm = rememberLauncherForActivityResult( + ActivityResultContracts.RequestPermission() + ) { granted -> + val src = internalFile + val fileName = exportFileName + if (granted && src != null) { + try { + val dst = saveToPublicDownloadsLegacy(context, src, fileName) + Log.i("AVIFEncoder", "인코딩 완료!\nDownloads에 저장됨: ${dst.absolutePath}\n앱 내부: ${src.absolutePath}") + } catch (t: Throwable) { + Log.e("AVIFEncoder", "저장 실패(28 이하): ${t.localizedMessage}") + + } finally { + internalFile = null + } + } else { + Log.e("AVIFEncoder", "저장 권한 거부됨 (API 28)") + internalFile = null + } + } + + try { + // 입력 비트맵 로드 + val bitmap = context.contentResolver.openInputStream(uri)?.use { ins -> + BitmapFactory.decodeStream(ins) + } + if (bitmap == null) { + Log.e("AVIFEncoder", "error occurred loading bitmap") + return + } + + // ARGB_8888 보장 + val bmp = if (bitmap.config == Bitmap.Config.ARGB_8888) { + bitmap + } else { + bitmap.copy(Bitmap.Config.ARGB_8888, false) + } + + // 픽셀 버퍼 추출 + val width = bmp.width + val height = bmp.height + val byteCount = bmp.rowBytes * height + val buffer = ByteBuffer.allocateDirect(byteCount).apply { + order(ByteOrder.nativeOrder()) + } + bmp.copyPixelsToBuffer(buffer) + buffer.rewind() + + // JNI 인코딩 + val avifBytes = AvifCodec.encodeRGBA8888(buffer, buffer.remaining(), width, height) + if (avifBytes == null) { + Log.e("AVIFEncoder", "error occurred while encoding to avif") + return + } + + + + // 내부 저장소에 먼저 기록 (임시 파일도 같은 이름으로) + val outputFile = File(context.filesDir, exportFileName) + FileOutputStream(outputFile).use { it.write(avifBytes) } + internalFile = outputFile + + // API 분기 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + // Android 10+ : MediaStore(Downloads)로 퍼미션 없이 저장 + val savedUri = saveToDownloadsViaMediaStore(context, outputFile, exportFileName) + Log.i("AVIFEncoder", "인코딩 완료!\nDownloads에 저장됨: $savedUri\n앱 내부: ${outputFile.absolutePath}") + internalFile = null + } else { + // Android 9 이하 : 권한 요청 후 /sdcard/Download에 저장 + requestWritePerm.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE) + // 콜백에서 statusText 설정 및 internalFile 해제 + } + + } catch (e: Exception) { + e.printStackTrace() + Log.i("AVIFEncoder", "오류 발생: ${e.localizedMessage}") + internalFile = null + } +} @Composable fun EncodeAvifScreen(modifier: Modifier = Modifier) { @@ -218,7 +314,7 @@ fun EncodeAvifScreen(modifier: Modifier = Modifier) { val height = bmp.height val byteCount = bmp.rowBytes * height val buffer = ByteBuffer.allocateDirect(byteCount).apply { - order(java.nio.ByteOrder.nativeOrder()) + order(ByteOrder.nativeOrder()) } bmp.copyPixelsToBuffer(buffer) buffer.rewind() @@ -323,7 +419,7 @@ fun saveToPublicDownloadsLegacy(context: Context, src: File, fileName: String = FileOutputStream(dst).use { output -> input.copyTo(output) } } // 파일 앱/다운로드 앱에서 즉시 보이도록 스캔 - android.media.MediaScannerConnection.scanFile( + MediaScannerConnection.scanFile( context, arrayOf(dst.absolutePath), arrayOf("image/avif"), null ) return dst diff --git a/app/src/main/java/com/example/ble_demo_app_peripheral_2/AvifCodec.kt b/app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifCodec.kt similarity index 98% rename from app/src/main/java/com/example/ble_demo_app_peripheral_2/AvifCodec.kt rename to app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifCodec.kt index 2cff646..f7070a1 100644 --- a/app/src/main/java/com/example/ble_demo_app_peripheral_2/AvifCodec.kt +++ b/app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifCodec.kt @@ -1,9 +1,8 @@ -package com.example.ble_demo_app_peripheral_2 +package com.example.ble_demo_app_peripheral_2.avif import android.graphics.Bitmap import java.nio.ByteBuffer - /** An AVIF Decoder. AVIF Specification: https://aomediacodec.github.io/av1-avif/. */ object AvifCodec { init { diff --git a/app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifUtils.kt b/app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifUtils.kt new file mode 100644 index 0000000..f132cdb --- /dev/null +++ b/app/src/main/java/com/example/ble_demo_app_peripheral_2/avif/AvifUtils.kt @@ -0,0 +1,92 @@ +package com.example.ble_demo_app_peripheral_2.avif + +import android.content.ContentResolver +import android.content.Context +import android.database.Cursor +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.net.Uri +import android.provider.OpenableColumns +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** 결과: 인코딩된 AVIF 바이트와 다운로드/전송에 쓸 파일명 (*.avif) */ +data class EncodedAvif(val bytes: ByteArray, val fileName: String) + +/** Uri의 표시 이름을 얻는다. (없으면 "image") */ +fun queryDisplayName(context: Context, uri: Uri): String { + return context.contentResolver.query( + uri, + arrayOf(OpenableColumns.DISPLAY_NAME), + null, null, null + )?.use { c -> + if (c.moveToFirst()) c.getString(0) ?: "image" else "image" + } ?: "image" +} + +/** Bitmap을 ARGB_8888로 보장해서 리턴 (필요 시 copy) */ +private fun ensureArgb8888(src: Bitmap): Bitmap { + return if (src.config == Bitmap.Config.ARGB_8888) src + else src.copy(Bitmap.Config.ARGB_8888, /* mutable = */ false) +} + +/** + * Uri에서 이미지를 로드하여 AVIF로 인코딩. + * - 디스크에 쓰지 않고 메모리에서 바로 인코딩 + * - 파일명은 원본 이름 기반으로 ".avif"로 바꿔서 생성 + */ +fun encodeAvifFromUri(context: Context, uri: Uri): EncodedAvif { + val resolver = context.contentResolver + + // 1) 이미지 로드 + val bmp0 = resolver.openInputStream(uri)?.use { ins -> + BitmapFactory.decodeStream(ins) + } ?: error("Failed to decode bitmap from uri: $uri") + + val bmp = ensureArgb8888(bmp0) + + // 2) 픽셀 버퍼 추출 (RGBA8888) + val width = bmp.width + val height = bmp.height + val byteCount = bmp.rowBytes * height + val buf = ByteBuffer.allocateDirect(byteCount).apply { + order(ByteOrder.nativeOrder()) + } + bmp.copyPixelsToBuffer(buf) + buf.rewind() + + // 3) JNI 인코딩 (AvifCodec.encodeRGBA8888) + val avifBytes = AvifCodec.encodeRGBA8888(buf, buf.remaining(), width, height) + ?: error("AVIF encoding failed") + + // 4) 파일명 생성 (*.avif) + val original = queryDisplayName(context, uri) + val base = original.substringBeforeLast('.', original) + val fileName = if (base.isBlank()) "image.avif" else "$base.avif" + + return EncodedAvif(avifBytes, fileName) +} + +/** + * 이미 로드된 Bitmap을 AVIF로 인코딩하고 파일명을 돌려준다. + * (직접 Bitmap을 다루는 경로가 필요할 때 사용) + */ +fun encodeAvifFromBitmap(bitmap: Bitmap, suggestedName: String = "image"): EncodedAvif { + val bmp = ensureArgb8888(bitmap) + + val width = bmp.width + val height = bmp.height + val byteCount = bmp.rowBytes * height + val buf = ByteBuffer.allocateDirect(byteCount).apply { + order(ByteOrder.nativeOrder()) + } + bmp.copyPixelsToBuffer(buf) + buf.rewind() + + val avifBytes = AvifCodec.encodeRGBA8888(buf, buf.remaining(), width, height) + ?: error("AVIF encoding failed") + + val base = suggestedName.substringBeforeLast('.', suggestedName).ifBlank { "image" } + val fileName = "$base.avif" + return EncodedAvif(avifBytes, fileName) +} diff --git a/app/src/main/java/com/example/ble_demo_app_peripheral_2/ble/CentralManager.kt b/app/src/main/java/com/example/ble_demo_app_peripheral_2/ble/CentralManager.kt index 0d16500..80fa298 100644 --- a/app/src/main/java/com/example/ble_demo_app_peripheral_2/ble/CentralManager.kt +++ b/app/src/main/java/com/example/ble_demo_app_peripheral_2/ble/CentralManager.kt @@ -7,10 +7,7 @@ import android.bluetooth.le.* import android.content.Context import android.content.pm.PackageManager import android.os.Build -import android.os.Handler -import android.os.Looper import android.os.ParcelUuid -import android.util.Log import androidx.core.app.ActivityCompat import com.inhandplus.shared.ble.BleSpec import kotlinx.coroutines.* @@ -187,7 +184,7 @@ class CentralManager( // 3) sendImage()에서 위 함수 사용 + 데이터 write 실패에도 재시도 경로 유지 @SuppressLint("MissingPermission") - fun sendImage(input: java.io.InputStream) { + fun sendImage(input: InputStream, fileName: String?) { val g = gatt ?: run { progressText.value = "Not connected"; return } if (rxWrite == null || connState.value != "Ready") { progressText.value = "Not ready (notify not enabled)" @@ -204,7 +201,7 @@ class CentralManager( percent.value = 0 // ★ 안정화된 Start frame 전송 - val started = sendStartFrameWithStabilize(g, totalLen, wholeCrc.value.toInt()) + val started = sendStartFrameWithStabilize(g, totalLen, wholeCrc.value.toInt(), fileName = fileName) if (!started) { progressText.value = "Start frame write failed" return@launch @@ -251,6 +248,78 @@ class CentralManager( } } + @SuppressLint("MissingPermission") + fun sendByteImage(bytes: ByteArray, fileName: String) { + val g = gatt ?: run { progressText.value = "Not connected"; return } + val w = rxWrite ?: run { progressText.value = "Not ready (no RX char)"; return } + if (connState.value != "Ready") { progressText.value = "Not ready (notify not enabled)"; return } + if (sending.getAndSet(true)) return + + scope.launch(kotlinx.coroutines.Dispatchers.IO) { + try { + // 전체 길이/CRC 초기화 + totalLen = bytes.size.toLong() + wholeCrc = java.util.zip.CRC32().apply { update(bytes) } + sentBytes = 0 + percent.value = 0 + + // Start 프레임 (ATT 20B, 확장자 5자 포함) 전송 + val started = sendStartFrameWithStabilize( + g = g, + totalLen = totalLen, + wholeCrc = wholeCrc.value.toInt(), // 시그니처 유지(내부에선 미사용) + fileName = fileName + ) + if (!started) { + progressText.value = "Start frame write failed" + return@launch + } + + // 데이터 전송 루프 + var seq = 0L + val payloadMax = calcSafePayload() + var offset = 0 + while (offset < bytes.size && sending.get()) { + // 크레딧 윈도우 대기 + waitWindow() + + val remain = bytes.size - offset + val chunk = if (remain > payloadMax) payloadMax else remain + val payload = bytes.copyOfRange(offset, offset + chunk) + val crc = java.util.zip.CRC32().apply { update(payload) }.value.toInt() + + val packet = java.nio.ByteBuffer.allocate(10 + chunk) + .order(java.nio.ByteOrder.LITTLE_ENDIAN) + .putInt(seq.toInt()) // u32 seq + .putShort(chunk.toShort()) // u16 len + .putInt(crc) // u32 crc + .put(payload) + .array() + + val ok = gattWriteCharacteristicSafe( + g, w, packet, + android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE + ) + if (!ok) { + progressText.value = "writeCharacteristic=false (retry exhausted) at seq=$seq" + break + } + + sentBytes += chunk + offset += chunk + seq++ + + percent.value = (100.0 * sentBytes / totalLen).toInt() + progressText.value = "Sent $sentBytes / $totalLen (${percent.value}%)" + } + } catch (t: Throwable) { + progressText.value = "Error: ${t.message}" + } finally { + sending.set(false) + } + } + } + private fun calcSafePayload(): Int { val att = (mtuAgreed - 3).coerceAtLeast(20) @@ -258,16 +327,6 @@ class CentralManager( return payload.coerceAtMost(BleSpec.DEFAULT_PAYLOAD) } - /** Start frame: seq=FFFFFFFF, len=0, payload=[ totalLen(u64) | wholeCrc(u32) ] */ - @SuppressLint("MissingPermission") - private suspend fun sendStartFrame(g: BluetoothGatt, totalLen: Long, wholeCrc: Int) { - val body = ByteBuffer.allocate(12).order(ByteOrder.LITTLE_ENDIAN) - .putLong(totalLen).putInt(wholeCrc).array() - val header = ByteBuffer.allocate(10).order(ByteOrder.LITTLE_ENDIAN) - .putInt(-1).putShort(0).putInt(0).array() - val packet = header + body - gattWriteCharacteristicSafe(g, rxWrite!!, packet, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) - } private suspend fun waitWindow() { while (inCredits <= 0 && sending.get()) { @@ -345,32 +404,56 @@ class CentralManager( // 2) Start frame 전 안정화 지연 (CentralManager.kt) @SuppressLint("MissingPermission") - private suspend fun sendStartFrameWithStabilize(g: BluetoothGatt, totalLen: Long, wholeCrc: Int): Boolean { - // CCC 쓰기 직후 스택 안정화 (권장 50ms) + private suspend fun sendStartFrameWithStabilize( + g: BluetoothGatt, + totalLen: Long, + wholeCrc: Int, // 사용 안 함 (보내지 않음) + fileName: String? = "" // ★ 원본 파일명 전달 가능하면 넣기 + ): Boolean { + // CCC 직후 안정화 kotlinx.coroutines.delay(50) - val body = java.nio.ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN) - .putLong(totalLen) - .array() - val header = java.nio.ByteBuffer.allocate(10).order(ByteOrder.LITTLE_ENDIAN) - .putInt(-1) // seq = 0xFFFFFFFF (Start) - .putShort(8) // ★ len = 8 (payload 길이!) - .putInt(0) // crc 필드는 의미 없음(0) + // 확장자 추출 (최대 5자, 영숫자만) + val ext = fileName!!.substringAfterLast('.', "") + .lowercase() + .take(5) + .filter { it.isLetterOrDigit() } + + // 파일 크기 24-bit 제한 + val total = totalLen.toInt() + if (totalLen > 0xFFFFFFL) { + progressText.value = "File too large for 24-bit len: $totalLen" + return false + } + + // Start Body (10B): ver(1) | totalLen24(3,LE) | extLen(1) | ext[5] + val body = ByteArray(10).apply { + this[0] = 1 // ver + this[1] = ( total and 0xFF).toByte() + this[2] = ((total shr 8) and 0xFF).toByte() + this[3] = ((total shr 16)and 0xFF).toByte() + this[4] = ext.length.toByte() + for (i in 0 until 5) this[5 + i] = if (i < ext.length) ext[i].code.toByte() else 0 + } + + // Start Header (10B) + val header = java.nio.ByteBuffer.allocate(10).order(java.nio.ByteOrder.LITTLE_ENDIAN) + .putInt(-1) // seq = 0xFFFFFFFF + .putShort(10) // len = 10 (payload length) + .putInt(0) // crc (unused) .array() + val packet = header + body - - - val writeStartPacketResult = gattWriteCharacteristicSafe( + val ok = gattWriteCharacteristicSafe( g, rxWrite!!, packet, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ) - if (!writeStartPacketResult) { - progressText.value = "staet write fail" - } - return writeStartPacketResult + if (!ok) progressText.value = "start write fail" + return ok } + @SuppressLint("MissingPermission") private fun writeDescriptorSafe(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, value: ByteArray) { if (Build.VERSION.SDK_INT >= 33) {