What Is The Best Way To Serialize An Image (compatible With Swing) From Java To Android?
I'm developing an Android app which is a quiz. On the other hand, I'm developing a desktop tool that is based entirely on Swing. The desktop tool is used to inserts the quiz's ques
Solution 1:
Here is the solution: Use BufferedImage
on Java side and convert it to byte array, then on the Android side, get the byte array and convert it to Bitmap
.
Java side:
publicstaticbyte[] imageToByteArray(BufferedImage image) throws IOException
{
ByteArrayOutputStreambaos=newByteArrayOutputStream();
ImageIO.write(image, "png", baos);
return baos.toByteArray();
}
/*
public static BufferedImage byteArrayToImage(byte[] imageArray) throws IOException
{
return ImageIO.read(new ByteArrayInputStream(imageArray));
}
*/
Android side:
BitmapFactory.Optionsopt=newBitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
byte[] imageByteArray = getImageByteArray();
Bitmapbitmap= BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length, opt);
imageView.setImageBitmap(bitmap);
Solution 2:
Use ImageIO to write the image to "png" or "jpg". e.g. http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html
Post a Comment for "What Is The Best Way To Serialize An Image (compatible With Swing) From Java To Android?"