如何显示JPEG图像直接从字节数组(之前保存图像)? [英] How to display the JPEG Image directly from byte array (Before saving Image)?

查看:491
本文介绍了如何显示JPEG图像直接从字节数组(之前保存图像)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个JPEG图像(图像尺寸:50KB)从客户端套接字,并在模拟器SD卡保存。从那里,我在显示的ImageView的jpg图片。但我想在SD卡存储图像,因为我们的Andr​​oid APPLI从插座接收连续图像,如果我按照接收,存储和显示方法,那么它会变得非常缓慢的过程之前,展示形象,所以要增加我想要的速度仅RAM显示器。为此,我需要暂时保存图像阵列上的RAM。从那里,我刨显示,通过使用单独的线程保存。所以,请指导我如何显示来自字节数组的形象。

注:我从插座接收JPEG图像,而不是.BMP或.gif或.png

下面是我的code从TCP套接字接收图像。 (其工作罚款)
(注:这是在单独的线程中完成的,不要尝试在UI线程)

 字节公众[] = mybytearray新的字节[310000]。
                    私人INT读取动作= 0;
                    私人INT电流= 0;                    ServerSocket的ServerSocket的=新的ServerSocket(SERVERPORT);
                    客户端的Socket的ServerSocket.accept =();
                   尝试{                       MYDIR =新的文件(到/ mnt / SD卡/ saved_images);                        如果(!myDir.exists()){
                            myDir.mkdir();
                        }其他{
                            Log.d(ServerActivity,文件夹已创建);
                        }                        字符串fpath =/image0001.jpg;
                        档案文件=新的文件(MYDIR,fpath);
                        如果(file.exists())file.delete();
                        InputStream为= client.getInputStream();
                        FOS的FileOutputStream =新的FileOutputStream(文件);
                        BOS的BufferedOutputStream =新的BufferedOutputStream(FOS);
                        读取动作= is.​​read(mybytearray,0,mybytearray.length);
                        电流=读取动作;                 做{
                      读取动作= is.​​read(mybytearray,电流,(mybytearray.length电流));
                      如果(读取动作> = 0)电流+ =读取动作;                 }而(读取动作-1个);                        bos.write(mybytearray,0,电流);                        Log.d(ServerActivity,从阵列图像复原);                        bos.flush();
                        bos.close();
                        fos.flush();
                        fos.close();
                        is.close();
                        client.close();
                        serverSocket.close();
                    }赶上(例外五){
                  e.printStackTrace();
              }


解决方案

尝试插入此code片段到code:

  {做
    读取动作= is.​​read(mybytearray,电流,(mybytearray.length电流));
    如果(读取动作> = 0)电流+ =读取动作;
}而(读取动作-1个);ByteArrayInputStream进行的InputStream =新ByteArrayInputStream进行(myByteArray);
位= BitmapFactory.de codeStream(InputStream的);
ImageView的照片=新ImageView的(本);
picture.setImageBitmap(位图);bos.write(mybytearray,0,电流);

I am receiving a jpeg image (Image size: 50KB) from client socket and saving in emulator SD Card. From there I am displaying the jpg image in Imageview. But I want to display the image before saving image on the SD Card because our android appli will receive the continous images from sockets, If I follow receive, save and display method then it will become very slow process, so to increase the speed I want display from ram only. For this I need to save the image array temporarily on the RAM. From there I planed to display and save by using the separate threads. So please guide me how to display the image from byte array.

Note: I am receiving JPEG image from socket, not .bmp or .gif or .png.

Below is my code for receiving the image from tcp socket. (Its working fine) (Note: This is done in seperate thread, don't try it in UI thread.)

                    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();
              }

解决方案

Try inserting this code snippet into your code:

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);

这篇关于如何显示JPEG图像直接从字节数组(之前保存图像)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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