Java 加载Android';SWT组件中的s图像(字节数组格式)

Java 加载Android';SWT组件中的s图像(字节数组格式),java,android,image,swt,bytearray,Java,Android,Image,Swt,Bytearray,这是一个场景: 在Android操作系统中加载图像-->在字节数组中转换-->将其发送到基于SWT的应用程序-->将其加载到组件中(我不知道,标签,按钮…) 我正确地接收到字节数组,但我不能在图像中转换它 这是我在net中找到的唯一代码,可以工作: byte[] bs = { (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x11, (byte) 0x11, (byte)

这是一个场景: 在Android操作系统中加载图像-->在字节数组中转换-->将其发送到基于SWT的应用程序-->将其加载到组件中(我不知道,标签,按钮…)

我正确地接收到字节数组,但我不能在图像中转换它

这是我在net中找到的唯一代码,可以工作:

byte[] bs = { (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x00, (byte) 0x00,
              (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x10, (byte) 0x00, (byte) 0x01,
              (byte) 0x10, (byte) 0x00, (byte) 0x01, (byte) 0x11, (byte) 0x11, (byte) 0x00, (byte) 0x22,
              (byte) 0x01, (byte) 0x10, (byte) 0x33, (byte) 0x00, (byte) 0x11, (byte) 0x10, (byte) 0x02,
              (byte) 0x22, (byte) 0x01, (byte) 0x10, (byte) 0x33, (byte) 0x30, (byte) 0x01, (byte) 0x10,
              (byte) 0x22, (byte) 0x22, (byte) 0x01, (byte) 0x10, (byte) 0x33, (byte) 0x33, (byte) 0x01,
              (byte) 0x10, (byte) 0x22, (byte) 0x22, (byte) 0x01, (byte) 0x10, (byte) 0x33, (byte) 0x33,
              (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
              (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x11, (byte) 0x11, (byte) 0x01, (byte) 0x10,
              (byte) 0x11, (byte) 0x11, (byte) 0x10, (byte) 0x01, (byte) 0x11, (byte) 0x11, (byte) 0x01,
              (byte) 0x10, (byte) 0x11, (byte) 0x11, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00,
              (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x44,
              (byte) 0x44, (byte) 0x01, (byte) 0x10, (byte) 0x55, (byte) 0x55, (byte) 0x01, (byte) 0x10,
              (byte) 0x44, (byte) 0x44, (byte) 0x01, (byte) 0x10, (byte) 0x55, (byte) 0x55, (byte) 0x01,
              (byte) 0x10, (byte) 0x04, (byte) 0x44, (byte) 0x01, (byte) 0x10, (byte) 0x55, (byte) 0x50,
              (byte) 0x01, (byte) 0x11, (byte) 0x00, (byte) 0x44, (byte) 0x01, (byte) 0x10, (byte) 0x55,
              (byte) 0x00, (byte) 0x11, (byte) 0x11, (byte) 0x10, (byte) 0x00, (byte) 0x01, (byte) 0x10,
              (byte) 0x00, (byte) 0x01, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x00,
              (byte) 0x00, (byte) 0x11, (byte) 0x11, (byte) 0x11, };*/

Color white = display.getSystemColor(SWT.COLOR_WHITE);
Color black = display.getSystemColor(SWT.COLOR_BLACK);
Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
Color red = display.getSystemColor(SWT.COLOR_RED);
Color green = display.getSystemColor(SWT.COLOR_GREEN);
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
// Create a source ImageData of depth 4
PaletteData palette = new PaletteData(new RGB[] { black.getRGB(), white.getRGB(),
yellow.getRGB(), red.getRGB(), blue.getRGB(), green.getRGB() });



ImageData sourceData = new ImageData(16, 16, 4, palette, 1, b);

但当我更改字节数组时,它不起作用。在这种情况下,如何设置PaletteData和ImageData???

我认为您应该能够使用
ImageIO.read
解析图像:

InputStream in = new ByteArrayInputStream(imageDataByteArray);
BufferedImage bufImg = ImageIO.read(in);
ImageIcon icon = new ImageIcon(bufImg);
myButton.setIcon(icon);
由于
BufferedImage
java.awt.Image
的一个子类,您应该能够按原样使用它。我自己没有测试过,但我不明白为什么它不起作用

还请注意,不能随机更改字节数组并期望它表示有效的图像。如果提供的数组不包含有效的图像数据(或包含不支持格式的图像),
ImageIO.read
将返回
null
。查看更多信息。

好的,正确的方式是:

contact.setImage(pCur.getBlob(0));
publishProgress("befor compress");
Bitmap image = BitmapFactory.decodeByteArray(contact.getImage(), 0, contact.getImage().length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 50, stream); //JPEG or what you want
contact.setImage(stream.toByteArray());
stream.close();

无论如何,我不能发送格式良好的字节数组。我发送约1700字节的字节数组,接收约2800个元素的数组。我不知道为什么。我用UTF-8字符串编码。但打印文件中的数组在本地工作。

这是引发的异常:java.lang.arrayindexoutofbounds异常:14我在最终图像源=新图像(display,sourceData)上的代码和平后收到它;这种代码的和平使用AWT和swing对象。我需要一个org.eclipse.swt.graphics。Image@user1417608查看如何将
BufferedImage
转换为
swt.Image
不可能。我尝试了swing/AWT对象,但在实例化BufferImage后它仍然为空。InputStream似乎有效…@user1417608问题可能与数据有关。如果字节数组不包含有效的图像数据,您将得到
null
。我可以使用发布的第一种方法绘制它。顺便说一下,我不能推广那种方法。如果我更改字节数组,
java.lang.ArrayIndexOutOfBoundsException:14
将引发。而SWT端,只需
final byte[]b=getContent().getBytes(“ISO-8859-1”);最终输入流tmp_in=新的ByteArrayInputStream(b,0,b.长度);图像tmp_i=新图像(显示,tmp_in)
****It's work for me****

String filepath = "/sdcard/Image/Image01.jpg";
           File imagefile = new File(filepath);
            FileInputStream fis = null;
            try 
            {
                fis = new FileInputStream(imagefile);
            } 
            catch (FileNotFoundException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Bitmap bm = BitmapFactory.decodeStream(fis);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            bm.compress(Bitmap.CompressFormat.PNG, 100 , baos);    
            byte[] b = baos.toByteArray(); 
            System.out.println(baos);
            Bitmap bm1=BitmapFactory.decodeByteArray(b,0, b.length);
            imgViewBitmap.setImageBitmap(bm1);