4

クライアント ソケットから jpeg イメージ (イメージ サイズ: 50KB) を受信し、エミュレータの SD カードに保存しています。そこから、jpg 画像を Imageview に表示しています。しかし、私たちのAndroidアプリはソケットから連続した画像を受信するため、SDカードに画像を保存する前に画像を表示したいのですが、受信、保存、表示の方法に従うと非常に遅いプロセスになるので、速度を上げる必要がありますRAMからのみ表示します。このために、イメージ配列を一時的に RAM に保存する必要があります。そこから別スレッドで表示・保存する予定でした。バイト配列から画像を表示する方法を教えてください。

注: .bmp、.gif、または .png ではなく、ソケットから JPEG 画像を受信して​​います。

以下は、TCPソケットから画像を受信するための私のコードです。(正常に動作しています)(注:これは別のスレッドで行われます。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();
              }
4

3 に答える 3

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);
于 2013-01-10T12:14:27.600 に答える
5

ビットマップを使用し、バイト配列から作成し、

Bitmap bitmap;
bitmap= BitmapFactory.decodeByteArray(mybytearray, 0, mybytearray.length);
于 2013-01-10T12:13:39.423 に答える
-1

byte[] をビットマップに変換します。以下を試してください

ByteArrayInputStream imageStream = new ByteArrayInputStream(byte[] array);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
于 2013-01-10T12:13:03.427 に答える