Skip to content

Commit 76689fb

Browse files
committed
Some fixes
1 parent ba777c1 commit 76689fb

File tree

8 files changed

+81
-7
lines changed

8 files changed

+81
-7
lines changed

app/src/main/assets/test_1.jpg

274 KB
Loading

app/src/main/assets/test_2.jpg

6.97 MB
Loading

app/src/main/assets/test_3.jpg

2 MB
Loading

app/src/main/assets/test_4.jpg

4.02 MB
Loading

app/src/main/assets/test_5.jpg

5.36 MB
Loading

app/src/main/java/com/radzivon/bartoshyk/avif/MainActivity.kt

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.graphics.BitmapFactory
55
import androidx.appcompat.app.AppCompatActivity
66
import android.os.Bundle
77
import android.util.Log
8+
import android.util.Size
89
import com.radzivon.bartoshyk.avif.coder.HeifCoder
910
import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
1011
import okio.buffer
@@ -31,8 +32,33 @@ class MainActivity : AppCompatActivity() {
3132
val decodedBitmap = BitmapFactory.decodeResource(resources, R.drawable.test_png)
3233
binding.imageView.setImageBitmap(decodedBitmap)
3334
binding.imageView.setImageBitmap(bitmap)
34-
val bytes = HeifCoder().encodeAvif(decodedBitmap)
35-
val ff = File(this.filesDir, "result.avif")
35+
// val bytes = HeifCoder().encodeAvif(decodedBitmap)
36+
// val ff = File(this.filesDir, "result.avif")
37+
// ff.delete()
38+
// val output = FileOutputStream(ff)
39+
// output.sink().buffer().use {
40+
// it.write(bytes)
41+
// it.flush()
42+
// }
43+
// output.close()
44+
// Log.d("p", bytes.size.toString())
45+
// writeHevc(decodedBitmap)
46+
val numbers = IntArray(5) { 1 * (it + 1) }
47+
numbers.forEach {
48+
testEncoder("test_${it}.jpg")
49+
}
50+
}
51+
52+
private fun testEncoder(assetName: String) {
53+
val buffer = this.assets.open(assetName).source().buffer().readByteArray()
54+
val opts = BitmapFactory.Options()
55+
opts.inMutable = true
56+
val bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.size, opts)
57+
// val newBitmap = bitmap.aspectFit(bitmap.width, bitmap.height)
58+
// bitmap.recycle()
59+
val bytes = HeifCoder().encodeAvif(bitmap)
60+
bitmap.recycle()
61+
val ff = File(this.filesDir, "${File(assetName).nameWithoutExtension}.avif")
3662
ff.delete()
3763
val output = FileOutputStream(ff)
3864
output.sink().buffer().use {
@@ -41,7 +67,43 @@ class MainActivity : AppCompatActivity() {
4167
}
4268
output.close()
4369
Log.d("p", bytes.size.toString())
44-
writeHevc(decodedBitmap)
70+
}
71+
72+
fun Bitmap.aspectFit(maxWidth: Int, maxHeight: Int): Bitmap {
73+
val image = this
74+
val width: Int = image.width
75+
val height: Int = image.height
76+
val ratioBitmap = width.toFloat() / height.toFloat()
77+
val ratioMax = maxWidth.toFloat() / maxHeight.toFloat()
78+
79+
var finalWidth = maxWidth
80+
var finalHeight = maxHeight
81+
if (ratioMax > ratioBitmap) {
82+
finalWidth = (maxHeight.toFloat() * ratioBitmap).toInt()
83+
} else {
84+
finalHeight = (maxWidth.toFloat() / ratioBitmap).toInt()
85+
}
86+
return Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true)
87+
}
88+
89+
fun aspectScale(sourceSize: Size, dstSize: Size): Size {
90+
val isHorizontal = sourceSize.width > sourceSize.height
91+
val targetSize = if (isHorizontal) dstSize else Size(dstSize.height, dstSize.width)
92+
if (targetSize.width > sourceSize.width && targetSize.width > sourceSize.height) {
93+
return sourceSize
94+
}
95+
val imageAspectRatio = sourceSize.width.toFloat() / sourceSize.height.toFloat()
96+
val canvasAspectRation = targetSize.width.toFloat() / targetSize.height.toFloat()
97+
val resizeFactor: Float
98+
if (imageAspectRatio > canvasAspectRation) {
99+
resizeFactor = targetSize.width.toFloat() / sourceSize.width.toFloat()
100+
} else {
101+
resizeFactor = targetSize.height.toFloat() / sourceSize.height.toFloat()
102+
}
103+
return Size(
104+
(sourceSize.width.toFloat() * resizeFactor).toInt(),
105+
(sourceSize.height.toFloat() * resizeFactor).toInt()
106+
)
45107
}
46108

47109
private fun writeHevc(bitmap: Bitmap) {

avif-coder/src/main/cpp/coder.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ jint throwCantEncodeImageException(JNIEnv *env) {
3232
return env->ThrowNew(exClass, "");
3333
}
3434

35+
jint throwPixelsException(JNIEnv *env) {
36+
jclass exClass;
37+
exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/GetPixelsException");
38+
return env->ThrowNew(exClass, "");
39+
}
40+
3541
struct AvifMemEncoder {
3642
std::vector<char> buffer;
3743
};
@@ -64,22 +70,24 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
6470
return static_cast<jbyteArray>(nullptr);
6571
}
6672
heif_encoder_set_lossy_quality(encoder, quality);
67-
6873
AndroidBitmapInfo info;
6974
int ret;
7075
if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
71-
throwCantEncodeImageException(env);
76+
heif_context_free(ctx);
77+
throwPixelsException(env);
7278
return static_cast<jbyteArray>(nullptr);
7379
}
7480

7581
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
76-
throwCantEncodeImageException(env);
82+
heif_context_free(ctx);
83+
throwPixelsException(env);
7784
return static_cast<jbyteArray>(nullptr);
7885
}
7986

8087
void *addr;
8188
if ((ret = AndroidBitmap_lockPixels(env, bitmap, &addr)) != 0) {
82-
throwCantEncodeImageException(env);
89+
heif_context_free(ctx);
90+
throwPixelsException(env);
8391
return static_cast<jbyteArray>(nullptr);
8492
}
8593

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.radzivon.bartoshyk.avif.coder
2+
3+
class GetPixelsException: Exception("Can't get pixels from android bitmap") {
4+
}

0 commit comments

Comments
 (0)