簡體   English   中英

從 android 客戶端通過套接字發送后收到黑色圖像

[英]Black image is received after being sent over socket from android client

我的 android 應用程序和台式 PC 之間有一個 TCP 連接,其中 android 向磁盤發送單幀 ZF972F8318E57DB83C15ADD6 和 PC。 我的問題是圖像顯然已成功發送,因為它有 9.7KiB。 但是,當我嘗試可視化此圖像時,我得到一個黑色圖像,並且 Android Studio IDE 中沒有拋出明顯的錯誤。

在此處輸入圖像描述

發送圖像的 android 應用程序:

private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.frame_image);
private final OnClickListener mOnClickListener = new OnClickListener() {
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_camera:
                if (!Check.isFastClick()) {
                     return;
                  }
                  if (mCameraHandler != null) {
                     if (mCameraHandler.isOpened()) {
                          if (checkPermissionWriteExternalStorage()) {
                              Bitmap bitmap = Bitmap.createBitmap(mImageView.getWidth(), mImageView.getHeight(), Bitmap.Config.ARGB_8888);
                              ByteArrayOutputStream baos = new  ByteArrayOutputStream();
                                                

                              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                              byte[] array = baos.toByteArray();
                                      mCameraHandler.captureStill(MediaMuxerWrapper.getCaptureFile(Environment.DIRECTORY_DCIM, ".png").toString()); 
                              SendImageClient sendImageClient = new SendImageClient();
                              sendImageClient.execute(array);
                              }
                        }
                 }
            break;
        };
    public class SendImageClient extends AsyncTask<byte[], Void, Void> {
          @Override
           protected Void doInBackground(byte[]... voids) {
        
                try {
                    Socket socket= new Socket("192.168.0.14",9999);
        
                    OutputStream out = socket.getOutputStream();
                    DataOutputStream dataOutputStream= new DataOutputStream(out);
                    dataOutputStream.write(voids[0],0,voids[0].length);
                    dataOutputStream.close();
                    out.close();
                    socket.close();
        
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }

activity_main.xml

<com.serenegiant.widget.UVCCameraTextureView
    android:id="@+id/camera_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/menu_layout" />

<com.serenegiant.widget.AutoFitTextureView
    android:id="@+id/textureView"
    android:layout_width="480px"
    android:visibility="invisible"
    android:layout_toRightOf="@id/menu_layout"
    android:layout_height="640px" />

<ImageView
    android:id="@+id/frame_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/camera_view"
    android:layout_alignLeft="@id/camera_view"
    android:layout_alignRight="@id/camera_view"
    android:layout_alignTop="@id/camera_view" />

服務器腳本.py

from socket import *

port = 9999
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', port))
s.listen(1)
conn, addr = s.accept()
print("Connected by the ",addr)


with open('/home/pi/Desktop/frames_saved/image.jpg', 'wb') as file:
    while True:
        data = conn.recv(1024*8)
        if not data: break
        file.write(data)


conn.close() 

為什么我得到一個黑色圖像,我怎樣才能將 ImageView 中顯示的實際圖像發送到桌面?

此代碼僅創建一個空的 bitmap

Bitmap bitmap = Bitmap.createBitmap(mImageView.getWidth(), mImageView.getHeight(), Bitmap.Config.ARGB_8888);

從 ImageView 獲取位圖

ImageView mImageView = findViewById(R.id.image);
BitmapDrawable bitmapDrawable = (BitmapDrawable) mImageView .getDrawable();
Bitmap bitmap = bitmapDrawable .getBitmap();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM