diff --git a/api/current.txt b/api/current.txt index 9ea0ec34..3378d6fe 100644 --- a/api/current.txt +++ b/api/current.txt @@ -164,6 +164,12 @@ package androidx.core.graphics { method @RequiresApi(26) @ColorLong public static long toColorLong(int); } + public final class ImageDecoderKt { + ctor public ImageDecoderKt(); + method @RequiresApi(28) public static android.graphics.Bitmap decodeBitmap(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3 action); + method @RequiresApi(28) public static android.graphics.drawable.Drawable decodeDrawable(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3 action); + } + public final class MatrixKt { ctor public MatrixKt(); method public static android.graphics.Matrix rotationMatrix(float degrees, float px = "0.0f", float py = "0.0f"); diff --git a/src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt b/src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt new file mode 100644 index 00000000..6d252916 --- /dev/null +++ b/src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.graphics + +import android.graphics.Bitmap +import android.graphics.ImageDecoder +import org.junit.Assert.assertEquals +import org.junit.BeforeClass +import org.junit.Test +import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer + +class ImageDecoderTest { + + @Test fun decodeBitmap() { + val src = ImageDecoder.createSource(buffer) + val decodedBitmap = src.decodeBitmap { _, _ -> setTargetSize(10, 10) } + assertEquals(10, decodedBitmap.width) + assertEquals(10, decodedBitmap.height) + } + + @Test fun decodeDrawable() { + val src = ImageDecoder.createSource(buffer) + val decodedDrawable = src.decodeDrawable { _, _ -> setTargetSize(10, 10) } + assertEquals(10, decodedDrawable.intrinsicWidth) + assertEquals(10, decodedDrawable.intrinsicHeight) + } + + companion object { + private lateinit var buffer: ByteBuffer + + @BeforeClass + @JvmStatic + fun beforeClass() { + val stream = ByteArrayOutputStream().apply { + Bitmap + .createBitmap(1, 1, Bitmap.Config.ARGB_8888) + .compress(Bitmap.CompressFormat.JPEG, 100, this) + } + + buffer = ByteBuffer.wrap(stream.toByteArray()) + } + } +} diff --git a/src/main/java/androidx/core/graphics/ImageDecoder.kt b/src/main/java/androidx/core/graphics/ImageDecoder.kt new file mode 100644 index 00000000..e1802ff3 --- /dev/null +++ b/src/main/java/androidx/core/graphics/ImageDecoder.kt @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.graphics + +import android.graphics.Bitmap +import android.graphics.ImageDecoder +import android.graphics.ImageDecoder.ImageInfo +import android.graphics.ImageDecoder.Source +import android.graphics.drawable.Drawable +import androidx.annotation.RequiresApi + +/** + * Create a Bitmap from a Source + * + * @see ImageDecoder.decodeBitmap + */ +@RequiresApi(28) +inline fun ImageDecoder.Source.decodeBitmap( + crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit +): Bitmap { + return ImageDecoder.decodeBitmap(this) { decoder, info, source -> + decoder.action(info, source) + } +} + +/** + * Create a Drawable from a Source + * + * @see ImageDecoder.decodeDrawable + */ +@RequiresApi(28) +inline fun ImageDecoder.Source.decodeDrawable( + crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit +): Drawable { + return ImageDecoder.decodeDrawable(this) { decoder, info, source -> + decoder.action(info, source) + } +}