4

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

Daniele
  • 1,005
  • 9
  • 26
alcoceba
  • 421
  • 4
  • 18

5 Answers5

3

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.

mattg
  • 1,731
  • 1
  • 12
  • 20
0

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.

Community
  • 1
  • 1
Skies
  • 407
  • 2
  • 6
  • 1
    Yes, but if I use Bitmap then I CAN'T use it in my Java desktop applications, because to use Bitmap I need to use Android libraries. Do you understand me?? – alcoceba May 16 '12 at 15:42
  • ok, I understand the problem. I'm looking for an issue but I think you need to create your own BufferReader. :s – Skies May 16 '12 at 15:47
  • I know, but I would know if there is an easier and faster solution than create my "own image processing class" – alcoceba May 16 '12 at 15:51
0

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.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
  • Sounds good. If I understand you correctly, I have to include Andorid libraries (that includes Bitmap) and the Java System Library (that includes bufferedImage) both in my custom library? Then catch the exceptions using try/catch? Is it right? – alcoceba May 17 '12 at 14:36
0

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

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 06:06