@@ -5,6 +5,7 @@ import android.graphics.BitmapFactory
5
5
import androidx.appcompat.app.AppCompatActivity
6
6
import android.os.Bundle
7
7
import android.util.Log
8
+ import android.util.Size
8
9
import com.radzivon.bartoshyk.avif.coder.HeifCoder
9
10
import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
10
11
import okio.buffer
@@ -31,8 +32,33 @@ class MainActivity : AppCompatActivity() {
31
32
val decodedBitmap = BitmapFactory .decodeResource(resources, R .drawable.test_png)
32
33
binding.imageView.setImageBitmap(decodedBitmap)
33
34
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" )
36
62
ff.delete()
37
63
val output = FileOutputStream (ff)
38
64
output.sink().buffer().use {
@@ -41,7 +67,43 @@ class MainActivity : AppCompatActivity() {
41
67
}
42
68
output.close()
43
69
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
+ )
45
107
}
46
108
47
109
private fun writeHevc (bitmap : Bitmap ) {
0 commit comments