Android / C#Socket - Decode / Enconde String64 1帧后出错

时间:2016-08-19 16:09:45

标签: java c# android sockets bitmap

我创建了一个C#服务器,通过套接字将一个Bitmap发送到android客户端。该位图不断更新,因为它是一个视频源。

服务器C#

private void send_data() {
    ImageConverter converter = new ImageConverter();
    byte[] sendBytes = (byte[]) converter.ConvertTo(master.picturebox_master.Image, typeof(byte[]));

    string_master_frame = System.Text.Encoding.UTF8.GetString(sendBytes);
    string_master_frame = Convert.ToBase64String(sendBytes);

    data = string_master_frame + "\n";

    tcpServer1.Send(data);
}

客户端Android

@Override
protected Void doInBackground(Void... arg0) {
    Socket socket = null;
    try {
        socket = new Socket(dstAddress, dstPort);
        Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

        while (true) {
            valores[26] = r.nextLine();

            publishProgress(valores[26]);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {

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

    }

发送第一帧,Android客户端正确显示。但是当下一帧出现时,Android客户端崩溃。

错误:

  

处理:com.example.tiago.java_android,PID:826                                                                               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)                                                                                   在com.example.tiago.java_android.Cliente.onProgressUpdate(Cliente.java:228)                                                                                   在com.example.tiago.java_android.Cliente.onProgressUpdate(Cliente.java:28)                                                                                   在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:656)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:135)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5431)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:914)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

我虽然valores [26]数据已被破坏,但事实并非如此。我正确地得到了数据。 我使用http://codebeautify.org/base64-to-image-converter来检查我是否正确获取了数据。

有什么想法吗?

PS:我丢失了帐户,所以我不得不做这个。

位图master_bitmap;

byte[] decodedString = Base64.decode(values[26],Base64.NO_WRAP);
    master_bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
    if(master_bitmap != null)
    {
        try {
            master_frame.setImageBitmap(master_bitmap);
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
    }
    master_bitmap.recycle();


}

没有用。错误:

  

java.lang.RuntimeException:Canvas:尝试使用循环位图

1 个答案:

答案 0 :(得分:0)

您是否尝试过我之前解释过的try / catch块?

回想:

我遇到了同样的问题,我几乎可以肯定这会解决你的问题:

    String[] safe = values[26].split("=");
    byte[] decodedString = Base64.decode(safe[0],Base64.NO_WRAP);
    Bitmap master_bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
    master_frame.setImageBitmap(master_bitmap);
    master_bitmap.recycle(); //THIS LINE WAS ENOUGH TO FIX MY CODE

另外,不要忘记有效使用try / catch块。

在这里尝试{//代码} catch(IllegalArgumentException e){//代码在这里,如果你想要} (如有必要,添加更多的catch语句。)

我的完整代码以获得进一步的帮助:

byte[] bytearray = Base64.decode(dataIn);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytearray, 0,bytearray.length);
if (myBitmap!=null) {
  //some irrelevant code here to turn bitmap to a PImage (a Processing image class)
}
myBitmap.recycle();

这对我来说很有效。

试试这个:

try { 
  Bitmap master_bitmap; 
  master_bitmap=BitmapFactory.decodeByteArray(decodedString, 0, ‌​decodedString.‌​leng‌​th); 
  if (master_bitmap != null) {  //might be an unnecessary if condidition
    try { 
      if (((BitmapDrawable)master_frame.getDrawable()).getBitmap()!=null) { 
        ((BitmapDrawable)master_frame.getDrawable()).getBitmap().recycle();
      }
      master_frame.setImageBitmap(master_bitmap);
      //maybe try here: master_bitmap.recycle();
    } 
    catch(RuntimeException e) { 
      e.printStackTrace();
    }
  }
} 
catch (IllegalArgumentException e) { 
  e.printStackTrace();
}