什么是序列化的图像(与Swing兼容)从Java到Android的最佳途径? [英] What is the best way to serialize an image (compatible with Swing) from Java to Android?

查看:141
本文介绍了什么是序列化的图像(与Swing兼容)从Java到Android的最佳途径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序是一个小测验。在另一方面,我正在开发一个桌面工具,这完全是根据摆动。桌面工具用于插入测验的问题和产生它包含了所有在其上的问题,序列化对象的文件。我用 java.awt.Image中来认为,附着有问题的图像。

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 questions and produces a serialized object file which contains all the questions on it. I used java.awt.Image to hold an image that is attached with a question.

不幸的是,当我完成开发的桌面工具,去到Android的一面,我想通了,Android有没有<​​code> java.awt.Image中。 所以我的问题是,反正是有包括 java.awt.Image中中的Andr​​oid应用程序?或者是有另一个类在Java和Android与图像处理,除了支持Swing组件可用?或者至少是有替代解决我所面临的问题?

Unfortunately, when I've finished developing the desktop tool and go to the Android side, I figured out that Android has no java.awt.Image. So my question is, is there anyway to include the java.awt.Image within the Android app? or is there another class available in both Java and Android that deals with Image, besides supporting the Swing components? or at least, is there an alternative to solve the problem I've faced?

注:您可能想知道为什么我序列化对象,而不仅仅是获取从XML或数据库的问题。这是因为,我有一个需要有一个树形数据结构的问题类别;每个类别都有的旁边有一个子类的问题列表。

Notes: You may wonder why I'm serializing the object and not just fetching the questions from XML or database. That's because, I have a need to have a tree data structure as categories of the questions; each category has list of questions beside a sub-category.

推荐答案

下面是解决方案:使用的BufferedImage 在Java端,并将其转换为字节数组,然后在Android的一面,得到字节数组并将其转换为位图

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方面:

public static byte[] imageToByteArray(BufferedImage image) throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    return baos.toByteArray();
}

/*
public static BufferedImage byteArrayToImage(byte[] imageArray) throws IOException
{
    return ImageIO.read(new ByteArrayInputStream(imageArray));
}
*/

Android的一面:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
byte[] imageByteArray = getImageByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length, opt);
imageView.setImageBitmap(bitmap);

这篇关于什么是序列化的图像(与Swing兼容)从Java到Android的最佳途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆