implement Bitmap.compress()

This commit is contained in:
Julian Winkler
2025-02-10 18:04:19 +01:00
parent e9769c1587
commit f69cff7113
3 changed files with 38 additions and 0 deletions

View File

@@ -79,6 +79,14 @@ JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1get_1pixels
JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1copy_1to_1buffer
(JNIEnv *, jclass, jlong, jobject, jint, jint);
/*
* Class: android_graphics_Bitmap
* Method: native_save_to_png
* Signature: (J)[B
*/
JNIEXPORT jbyteArray JNICALL Java_android_graphics_Bitmap_native_1save_1to_1png
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}
#endif

View File

@@ -113,3 +113,15 @@ JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1copy_1to_1buffer(JNI
release_nio_buffer(env, array_ref, array);
gdk_texture_downloader_free(downloader);
}
JNIEXPORT jbyteArray JNICALL Java_android_graphics_Bitmap_native_1save_1to_1png(JNIEnv *env, jclass class, jlong texture_ptr)
{
GdkTexture *texture = GDK_TEXTURE(_PTR(texture_ptr));
GBytes *bytes = gdk_texture_save_to_png_bytes(texture);
jbyteArray result = (*env)->NewByteArray(env, g_bytes_get_size(bytes));
gsize size;
gconstpointer data = g_bytes_get_data(bytes, &size);
(*env)->SetByteArrayRegion(env, result, 0, size, data);
g_bytes_unref(bytes);
return result;
}

View File

@@ -1,5 +1,7 @@
package android.graphics;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import android.util.DisplayMetrics;
@@ -27,6 +29,11 @@ public final class Bitmap {
}
}
public enum CompressFormat {
JPEG,
PNG,
}
private int width;
private int height;
private int stride;
@@ -190,6 +197,16 @@ public final class Bitmap {
return true;
}
public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) throws IOException {
if (format == CompressFormat.PNG) {
stream.write(native_save_to_png(getTexture()));
return true;
} else {
stream.write(("fixme Bitmap.compress " + format.name()).getBytes());
return false;
}
}
@SuppressWarnings("deprecation")
@Override
protected void finalize() throws Throwable {
@@ -209,4 +226,5 @@ public final class Bitmap {
private static native long native_ref_texture(long texture);
private static native void native_get_pixels(long texture, int[] pixels, int offset, int stride, int x, int y, int width, int height);
private static native void native_copy_to_buffer(long texture, Buffer buffer, int memory_format, int stride);
private static native byte[] native_save_to_png(long texture);
}