如何直接从字节数组中显示JPEG图像(保存图像之前)?

时间:2013-01-10 12:08:02

标签: android image sockets jpeg

我从客户端套接字接收jpeg图像(图像大小:50KB)并保存在模拟器SD卡中。从那里我在Imageview中显示jpg图像。但是我想在将图像保存到SD卡之前显示图像,因为我们的android appli将从套接字接收连续图像,如果我按照接收,保存和显示方法那么它将变得非常慢的过程,所以提高我想要的速度仅从ram显示。为此,我需要将图像阵列临时保存在RAM上。从那里我计划通过使用单独的线程显示和保存。那么请指导我如何从字节数组中显示图像。

注意:我从套接字接收JPEG图像,而不是.bmp或.gif或.png。

以下是我从tcp socket接收图像的代码。 (工作正常) (注意:这是在单独的线程中完成的,不要在UI线程中尝试。)

                    public byte[] mybytearray  = new byte[310000];
                    private int bytesRead=0;
                    private int current = 0;

                    ServerSocket serverSocket = new ServerSocket(SERVERPORT);  
                    Socket client = serverSocket.accept(); 


                   try {

                       myDir=new File("/mnt/sdcard/saved_images");

                        if (!myDir.exists()){
                            myDir.mkdir();
                        }else{
                            Log.d("ServerActivity","Folder Already created" );
                        }

                        String fpath = "/image0001.jpg";
                        File file = new File (myDir, fpath);
                        if (file.exists ()) file.delete ();


                        InputStream is = client.getInputStream();
                        FileOutputStream fos = new FileOutputStream(file);
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        bytesRead = is.read(mybytearray,0,mybytearray.length);
                        current = bytesRead;

                 do {
                      bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
                      if(bytesRead >= 0) current += bytesRead;

                 } while(bytesRead > -1);

                        bos.write(mybytearray, 0 , current);

                        Log.d("ServerActivity","Reconstructing Image from array");

                        bos.flush();
                        bos.close();
                        fos.flush();
                        fos.close();
                        is.close();
                        client.close();
                        serverSocket.close();
                    } catch (Exception e) { 
                  e.printStackTrace();
              }

3 个答案:

答案 0 :(得分:10)

尝试将此代码段插入代码中:

do {
    bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

ByteArrayInputStream inputStream = new ByteArrayInputStream(myByteArray);
bitmap = BitmapFactory.decodeStream(inputStream);
ImageView picture = new ImageView(this);
picture.setImageBitmap(bitmap);

bos.write(mybytearray, 0 , current);

答案 1 :(得分:4)

使用Bitmap,从字节数组创建

Bitmap bitmap;
bitmap= BitmapFactory.decodeByteArray(mybytearray, 0, mybytearray.length);

答案 2 :(得分:-1)

将byte []转换为位图。 请尝试以下

ByteArrayInputStream imageStream = new ByteArrayInputStream(byte[] array);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);