How to load BufferedImage in android?
ImageIO is not supported in Android SDK
Could you achieve the same thing with Bitmap
and BitmapFactory
?? like so...
Bitmap tgtImg = BitmapFactory.decodeFile("ImageD2.jpg");
if tgtImg
is not null
after this then it was successful.
BufferedImage in Android
No. You can't use BufferedImage
because, like you said, javax.imageio
isn't in the Android SDK. The Bitmap
class, however, does support getting individual pixels using the getPixel()
and getPixels()
methods so you should be able to use those to do any type of image transform you want to do.
How can I import java.awt.image.BufferedImage in Android Studio
You can't.
The AWT package is not supported in Android, you need to change your implementation to use the Android classes.
See these similar questions:
Porting AWT graphics code to Android
How to add java AWT image package in Android
Using AWT with Android
Java BufferedImage / Android Bitmap
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.
BufferedImage class
BufferedImage belongs to the awt package and isn't available on Android :/
docs.oracle.com -> BufferedImage
However you can use the Bitmap class.It should get you to where you wanna go.
Android Developer - Bitmap
Android Developer - Guide for working with Bitmaps
Send Java BufferedImage to Bitmap Android
To elaborate on the suggestion I made in the comment:
From the Java/server side, send the image's width and height (if you know your image's type is always TYPE_4BYTE_ABGR
you don't need anything else):
BufferedImage image = msg.getImage();
byte[] imgBytes = ((DataBufferByte) image.getData().getDataBuffer()).getData();
// Using DataOutputStream for simplicity
DataOutputStream data = new DataOutputStream(out);
data.writeInt(image.getWidth());
data.writeInt(image.getHeight());
data.write(imgBytes);
data.flush();
Now you can either convert the interleaved ABGR byte array to packed int ARGB on the server side, or on the client side, it does not really matter. I'll show the conversion on the Android/client side, for simplicity:
// Read image data
DataInputStream data = new DataInputStream(in);
int w = data.readInt();
int h = data.readInt();
byte[] imgBytes = new byte[w * h * 4]; // 4 byte ABGR
data.readFully(imgBytes);
// Convert 4 byte interleaved ABGR to int packed ARGB
int[] pixels = new int[w * h];
for (int i = 0; i < pixels.length; i++) {
int byteIndex = i * 4;
pixels[i] =
((imgBytes[byteIndex ] & 0xFF) << 24)
| ((imgBytes[byteIndex + 3] & 0xFF) << 16)
| ((imgBytes[byteIndex + 2] & 0xFF) << 8)
| (imgBytes[byteIndex + 1] & 0xFF);
}
// Finally, create bitmap from packed int ARGB, using ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888);
If you really want ARGB_4444
, you can convert the bitmap, but note that the constant is deprecated in all recent versions of the Android API.
How can I import java.awt.BufferedImage in an Android project?
Any answers suggesting the use of BufferedImage in an Android environment are wrong; as you note, it's not part of the Android environment. However, you can gain the same efficiency effects by using a Bitmap.
Convert BufferedImage output from Java to a Bitmap in Android
@haraldK, thanks for giving me the right pointer. I was also wondering why I didn't see any padding, but didn't put enough focus on it to understand the issue. But, based on your hint, I finally figured out the issue. The issue was that I was not closing the OutputStream given by Base64 Encoder properly. In my code I had a the Base64 encoder stream as a wrapper of a ByteArrayOutputStream as below:
final ByteArrayOutputStream os = new ByteArrayOutputStream();
String qString = "";
try {
MatrixToImageWriter.writeToStream(matrix, formatName, Base64.getEncoder().wrap(os));
qString = os.toString("UTF-8");
os.close();
return qString;
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
In the above code, I was closing the ByteArrayOutputStream using os.close() which was not required. But what was required was closing of the OutputStream given out by the Base64 encoder. I changed the code as below to make it work:
final ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream base64Stream = Base64.getEncoder().wrap(os);
String qString = "";
try {
MatrixToImageWriter.writeToStream(matrix, formatName, base64Stream);
base64Stream.close();
qString = os.toString("UTF-8");
os.close();
return qString;
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
After the above change, the base64 encoded strings are properly padded and are properly getting decoded on the Android side. On the android side this is code I have to display the image:
byte[] imageArr=Base64.decode(base64String.getBytes(), Base64.NO_WRAP);
Bitmap bitmap=BitmapFactory.decodeByteArray(imageArr, 0, imageArr.length);
return bitmap;
I didn't know how to upvote your answer, so I have created this as a separate answer. Thanks for your help.
Creating a BufferedImage from a byte array java
Android does not have neither ImageIO
nor BufferedImage
. Here is an alternative: What is the best way to serialize an image (compatible with Swing) from Java to Android?
Related Topics
How to Check the Multiple Permission at Single Request in Android M
Android Gridview Row Dividers/Separators
How to Display More Than 3- Levels of Expandable List View
Play Sound Using Soundpool Example
Proguard Ignores Config File of Library
Android Alarmmanager Not Waking Phone Up
How to Hide Android Soft Keyboard on Edittext
Oauth 2.0 Authorization for Linkedin in Android
What Is a Full Android Database Helper Class for an Existing SQLite Database
How to Scroll to Bottom in a Scrollview on Activity Startup
How to Check If Facebook Is Installed Android
Livedata Prevent Receive the Last Value When Start Observing
Httpurlconnection Worked Fine in Android 2.X But Not in 4.1: No Authentication Challenges Found
Error: Configuration with Name 'Default' Not Found in Android Studio
Sharedpreferences and Thread Safety
Animation.Setfillafter/Before - Do They Work/What Are They For