简体   繁体   English

Android从C ++套接字接收图像

[英]Android receiving image from c++ socket

New to android application: Need some help I am trying to display the image that i have receive from my c++ socket. android应用程序的新功能:需要一些帮助,我正在尝试显示我从c ++套接字收到的图像。 I am able to receive the image from my server socket and it is displaying the image that i have receive fine. 我能够从服务器套接字接收图像,并且它显示我已经收到的图像。 But the emulator force closes after that and i have no idea why. 但是之后仿真器关闭,我不知道为什么。

Android code Android代码

private  class Connect extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params){
        int imageSize=921600;
        InputStream in;
        mRun = true;
        try{
            port1 = Integer.parseInt(port);
            client = new Socket(ip, port1);
            try{
                while(mRun){
                    in = client.getInputStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte buffer[] = new byte[1024];
                    int remainingBytes = imageSize; //
                    while (remainingBytes > 0) {
                        int bytesRead = in.read(buffer);
                        if (bytesRead < 0) {
                            throw new IOException("Unexpected end of data");
                        }
                        baos.write(buffer, 0, bytesRead);
                        remainingBytes -= bytesRead;
                    }
                    in.close();
                    imageByte = baos.toByteArray();   
                    baos.close();
                    int nrOfPixels = imageByte.length / 3; // Three bytes per pixel.
                    int pixels[] = new int[nrOfPixels];
                    for(int i = 0; i < nrOfPixels; i++) {
                        int r = imageByte[3*i];
                        int g = imageByte[3*i + 1];
                        int b = imageByte[3*i + 2];

                        if (r < 0) 
                            r = r + 256; 

                        if (g < 0) 
                            g = g + 256;

                        if (b < 0) 
                            b = b + 256;

                        pixels[i] = Color.rgb(b,g,r);
                    }
                    Bitmap bitmap = Bitmap.createBitmap(pixels, 640, 480,Bitmap.Config.ARGB_8888);
                    camera.setImageBitmap(bitmap);
                    camera.invalidate();
                }
            } catch (IOException e){}
        }
        catch (UnknownHostException e) {}
        catch (IOException e){}
        return null;
    }
}

emulator force closes after that and i have no idea why 之后,仿真器强制关闭,我不知道为什么

Because doInBackground method run on background thread but you are trying to set bitmap for images from doInBackground here : 因为doInBackground方法在后台线程上运行,但是您尝试在此处设置来自doInBackground图像的位图:

camera.setImageBitmap(bitmap);
camera.invalidate();

To update ImageView bitmap from main UI Thread use onPostExecute to call setImageBitmap and invalidate for ImageView because this method run on Main UI Thread when doInBackground method task is completed. 要从主UI线程更新ImageView位图,请使用onPostExecute来调用setImageBitmap invalidate ImageView invalidate ,因为doInBackground方法任务完成后,此方法将在主UI线程上运行。

OR 要么

Use runOnUiThread method to call setImageBitmap and invalidate method from doInBackground : 使用runOnUiThread方法调用setImageBitmap并从doInBackground runOnUiThread方法invalidate

      YourActivityName.this.runOnUiThread(new Runnable() {

           @Override
           public void run() {
              camera.setImageBitmap(bitmap);
              camera.invalidate();
            }
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM