【问题标题】:Android - Receving an Image over the Socket from C# Server (Almost Complete)Android - 通过套接字从 C# 服务器接收图像(几乎完成)
【发布时间】:2016-12-26 16:12:45
【问题描述】:

我在我的服务器上每秒发送 x 次图像(基本上是视频流)。

master.frame_drone.Bitmap 是图片

C# 服务器

 private void send_data()
    {

            string data = "";


            string conetado, bateria, tensao, altitude, roll, pitch, yaw, velx, vely, velz, estado, atual, desejado;
            string conetado2, bateria2, tensao2, altitude2, roll2, pitch2, yaw2, velx2, vely2, velz2, estado2, atual2, desejado2;

            conetado = master.sconetado;
            bateria = master.sbateria;
            tensao = master.stensao;
            altitude = master.saltitude;
            roll = master.sroll;
            pitch = master.spitch;
            yaw = master.syaw;
            velx = master.svelx;
            vely = master.svely;
            velz = master.svelz;
            estado = master.sestado;
            atual = master.satual;
            desejado = master.sdesejado;


            conetado2 = slave.sconetado;
            bateria2 = slave.sbateria;
            tensao2 = slave.stensao;
            altitude2 = slave.saltitude;
            roll2 = slave.sroll;
            pitch2 = slave.spitch;
            yaw2 = slave.syaw;
            velx2 = slave.svelx;
            vely2 = slave.svely;
            velz2 = slave.svelz;
            estado2 = slave.sestado;
            atual2 = slave.satual;
            desejado2 = slave.sdesejado;


            ImageConverter converter = new ImageConverter();
            byte[] sendBytes = (byte[])converter.ConvertTo(master.frame_drone.Bitmap, typeof(byte[]));

            string_master_frame = Convert.ToBase64String(sendBytes);

            data = conetado + "\n" + bateria + "\n" + tensao + "\n" + altitude + "\n" + roll + "\n" + pitch + "\n" + yaw + "\n" + velx + "\n" + vely + "\n" + velz + "\n" + estado + "\n" + atual + "\n" + desejado + "\n" +
                        conetado2 + "\n" + bateria2 + "\n" + tensao2 + "\n" + altitude2 + "\n" + roll2 + "\n" + pitch2 + "\n" + yaw2 + "\n" + velx2 + "\n" + vely2 + "\n" + velz2 + "\n" + estado2 + "\n" + atual2 + "\n" + desejado2 + "\n" + string_master_frame + "\n";

            tcpServer1.Send(data);


    }

所以我从服务器得到一个字符串,它基本上是图像。

安卓客户端

 protected Void doInBackground(Void... arg0) {


        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            while(true) {
                Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

     /*
      * notice: inputStream.read() will block if no data return
      */
                //MASTER
                valores[0] = r.nextLine();
                valores[1] = r.nextLine();
                valores[2] = r.nextLine();
                valores[3] = r.nextLine();
                valores[4] = r.nextLine();
                valores[5] = r.nextLine();
                valores[6] = r.nextLine();
                valores[7] = r.nextLine();
                valores[8] = r.nextLine();
                valores[9] = r.nextLine();
                valores[10] = r.nextLine();
                valores[11] = r.nextLine();
                valores[12] = r.nextLine();

                //SLAVE
                valores[13] = r.nextLine();
                valores[14] = r.nextLine();
                valores[15] = r.nextLine();
                valores[16] = r.nextLine();
                valores[17] = r.nextLine();
                valores[18] = r.nextLine();
                valores[19] = r.nextLine();
                valores[20] = r.nextLine();
                valores[21] = r.nextLine();
                valores[22] = r.nextLine();
                valores[23] = r.nextLine();
                valores[24] = r.nextLine();
                valores[25] = r.nextLine();

                valores[26] = r.nextLine();


                publishProgress(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8],valores[9],valores[10],valores[11],valores[12],
                        valores[13],valores[14],valores[15],valores[16],valores[17],valores[18],valores[19],valores[20],valores[21],valores[22],valores[23],valores[24],valores[25],valores[26]);
            }

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);
    conetado.setText(values[0]);
    bateria.setText(values[1]);
    tensao.setText(values[2]);
    altitude.setText(values[3]);
    roll.setText(values[4]);
    pitch.setText(values[5]);
    yaw.setText(values[6]);
    velx.setText(values[7]);
    vely.setText(values[8]);
    velz.setText(values[9]);
    estado.setText(values[10]);
    atual.setText(values[11]);
    desejado.setText(values[12]);

    conetado2.setText(values[13]);
    bateria2.setText(values[14]);
    tensao2.setText(values[15]);
    altitude2.setText(values[16]);
    roll2.setText(values[17]);
    pitch2.setText(values[18]);
    yaw2.setText(values[19]);
    velx2.setText(values[20]);
    vely2.setText(values[21]);
    velz2.setText(values[22]);
    estado2.setText(values[23]);
    atual2.setText(values[24]);
    desejado2.setText(values[25]);

    Bitmap master_bitmap = BitmapFactory.decodeByteArray(values[26].getBytes(),0,values[26].length());
    master_frame.setImageBitmap(master_bitmap);

}

}

我的问题是我使用\n 逐行读取字符串,所有TextViews 都运行良好,但是当我引入字符串valores[26] 时,所有内容都开始去同步并且所有值都混合在一起,例如:@987654327 @现在得到了valores[26]等的一些数据……

如何从服务器获取字符串并将其显示在ImageView 上?

编辑:

Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));
while(true) {

工作。现在数据没有混合。最后一个问题:

在我的 onProgressUpdate 中,我尝试解码字符串但没有成功。

    byte[] decodedString = Base64.decode(values[26],Base64.DEFAULT);
    Bitmap master_bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
    master_frame.setImageBitmap(master_bitmap);

进程:com.example.tiago.java_android,PID:3510 java.lang.IllegalArgumentException:bad base-64 在 android.util.Base64.decode(Base64.java:161) 在 android.util.Base64.decode(Base64.java:136) 在 android.util.Base64.decode(Base64.java:118)

【问题讨论】:

  • valores 是什么?
  • 字符串数组。只是为了从服务器获取数据
  • string_master_frame 将登陆价值 [???];哪一个?
  • ....... 价值[26]
  • 你能检查一下我的编辑吗?

标签: c# android image sockets


【解决方案1】:
     while(true) {
            Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

将其更改为:

    Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

    while(true) {

【讨论】:

  • 是的。你做。你保持循环。
  • 服务器主要发送无人机的导航数据(所有这些第一个参数)和图像(master.frame_drone)
  • 这看起来很奇怪,但我会尝试的。如果 Scanner 不在循环内,它怎么能一直读取?你可以解释吗?谢谢
  • 试试吧,你会看到的。扫描仪停留。
  • 哈哈,它奏效了。现在它不混合了。好的。但我有最后一个问题。你能帮助我吗?我会编辑。
猜你喜欢
  • 2015-03-22
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2017-02-25
  • 2015-05-18
  • 2020-09-17
相关资源
最近更新 更多