alcoceba
alcoceba

Reputation: 423

Java BufferedImage / Android Bitmap

I am developing a little library to work in both Android application and Java desktop application. I need to use BufferedImage to get RGB values from an image, but I can't use BufferedImage in Andorid. And viceversa with Bitmap.

Is there any way to implement BufferedImage in Android or to implement Bitmap in the Java desktop application??

Thanks

Upvotes: 4

Views: 9804

Answers (5)

YangBaoSheng
YangBaoSheng

Reputation: 1

maybe you need this: import androidx.compose.ui.graphics.toComposeImageBitmap

Upvotes: 0

mattg
mattg

Reputation: 1855

You could have your library not use either one, but some interface that you define that has all of the functions you need. Then for the Android version, you implement the interface using a Bitmap object, and for the desktop version, you implement the interface with a BufferedImage. The caller that uses your library passes in the implementation of the interface that corresponds to the platform the caller is using, and your code doesn't ever have to worry about platform specific stuff.

Of course, whether or not this is worth the effort depends on how extensively the image objects are used in your library. If it's just a line or two of code that needs to read the image, it may not be worth the trouble, and the reflection methods given in other answers might be easier.

Upvotes: 3

android developer
android developer

Reputation: 116060

use reflection to find the class to use in order to load the bitmap . when succeeded , use it from now on .

an alternative is to create your own image parser . here are some links for parsing png files:

http://www.java-gaming.org/index.php/topic,24202.

http://hg.l33tlabs.org/twl/file/tip/src/de/matthiasmann/twl/utils/PNGDecoder.java

http://www.java2s.com/Code/Java/2D-Graphics-GUI/PNGDecoder.htm

Upvotes: 0

Lawrence D'Oliveiro
Lawrence D'Oliveiro

Reputation: 2804

You can have wrapper code that tries to call the platform-specific libraries, and catches NoClassDefFoundError if they’re not present. This way you can dynamically determine the appropriate APIs to use.

Upvotes: 0

Skies
Skies

Reputation: 415

Have you try this ? : How to load BufferedImage in android?

When you have a Bitmap you can use the getPixel method to find the color.

Upvotes: 0

Related Questions