Really fix isApng function

This commit is contained in:
oupson 2018-11-16 19:22:15 +01:00
parent a9d61f90ba
commit ec67f6e09f
4 changed files with 16 additions and 5 deletions

View File

@ -9,9 +9,10 @@ Copyright (c) 2018 Miller Cy Chan
https://github.com/mcychan/nQuant.android/blob/master/nQuant.master/src/main/java/com/android/nQuant/PnnQuantizer.java
*/
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -18,13 +18,17 @@ class Utils {
* @return True if is an APNG
*/
fun isApng(byteArray: ByteArray) : Boolean {
if (!isPng(byteArray)) return false
try {
val acTL = byteArrayOf(0x66, 0x63, 0x54, 0x4c)
loop@ for (i in 0 until byteArray.size) {
val acTL = byteArrayOf(0x61, 0x63, 0x54, 0x4c)
val IDAT = byteArrayOf(0x49, 0x44, 0x41, 0x54)
for (i in 0 until byteArray.size) {
val it = byteArray.copyOfRange(i, i + 4)
// if byteArray contain acTL
if (it.contentEquals(acTL)) {
return true
} else if (it.contentEquals(IDAT)){
return false
}
}
return false

View File

@ -62,7 +62,13 @@ class Main2Activity : AppCompatActivity() {
}
} else {
try {
animator.load(getImageRealPath(contentResolver, uri, null))
val bytes = contentResolver.openInputStream(uri).readBytes()
if (isApng(bytes)) {
animator.load(bytes)
} else {
imageView3.setImageBitmap(BitmapFactory.decodeFile(getImageRealPath(contentResolver, uri, null)))
Snackbar.make(constraint, "Not an APNG", Snackbar.LENGTH_LONG).show()
}
} catch (e: NotApngException) {
imageView3.setImageBitmap(BitmapFactory.decodeFile(getImageRealPath(contentResolver, uri, null)))
Snackbar.make(constraint, "Not an APNG", Snackbar.LENGTH_LONG).show()

View File

@ -29,7 +29,7 @@ class MainActivity : AppCompatActivity() {
load(imageUrl)
onLoaded {
setOnAnimationLoopListener {
Log.e("app-test", "onLoop")
// Log.e("app-test", "onLoop")
}
}
}