简体   繁体   English

将Base64编码的图像作为字符串发送到Chromecast

[英]Send Base64 encoded image as string to Chromecast

The problem is to send local image from phone as encoded Base64 string to Chromecast. 问题是将电话中的本地图像作为编码的Base64字符串发送到Chromecast。 And decode it using my Custom Receiver. 并使用我的自定义接收器对其进行解码。 I was following this guide which is based on this project sample . 我正在遵循基于该项目样本的 指南。

I suggest the problem might be in: 我建议问题可能出在:

  1. Custom Receiver is not proper (I'm not strong in JS). 自定义接收器不合适(我不太擅长JS)。
  2. Chromecast didn't load that Receiver (I don't know how to check that). Chromecast未加载该Receiver(我不知道如何检查)。
  3. Image was encoded wrong on device or decoded on Chromecast. 图片在设备上编码错误或在Chromecast上解码。

You see, it seems like I coded everithing right since the status of Chromecast when I send photo is: 您会发现,由于我发送照片时Chromecast状态为:

 statusCode 0 (success), 
 application name: Default Media Receiver, 
 status: Ready To Cast, 
 sessionId: 34D6CE75-4798-4294-BF45-2F4701CE4782, 
 wasLaunched: true.

This is how I send image as String: 这就是我将图像作为String发送的方式:

mCastManager.castImage(mCastManager.getEncodedImage(currentEntryPictureByPoint.getPath()));

Methods used: 使用的方法:

public void castImage(String encodedImage)
{
    Log.d(TAG, "castImage()");
    String image_string = createJsonMessage(MessageType.image, encodedImage);
    sendMessage(image_string);
}

private static String createJsonMessage(MessageType type, String message)
{
    return String.format("{\"type\":\"%s\", \"data\":\"%s\"}", type.toString(), message);
}

/**
 * Convert Image to encoded String
 * */
public String getEncodedImage(String path){
    Bitmap bm = BitmapFactory.decodeFile(path);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    byte[] byteArrayImage = baos.toByteArray();

    String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

    return encodedImage;
}

/**
 * Send a text message to the receiver
 *
 * @param message
 */
private void sendMessage(String message) {
    if (mApiClient != null && mCustomImageChannel != null) {
        try {
            Cast.CastApi.sendMessage(mApiClient,
                    mCustomImageChannel.getNamespace(), message)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status result) {
                            if (!result.isSuccess()) {
                                //ALWAYS REACHING HERE :(
                                Log.e(TAG, "Sending message failed");
                            }
                        }
                    });
        } catch (Exception e) {
            Log.e(TAG, "Exception while sending message", e);
        }
    } else {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT)
                .show();
    }
}

If the sending process is correct then the Receiver is wrong and don't know how to decode this message properly. 如果发送过程正确,那么接收方是错误的,并且不知道如何正确解码此消息。 The way I uploaded it (well, at least I think that its uploaded...) 上传它的方式(嗯,至少我认为它上传了...)

  1. Registered new Custom Receiver on Google Cast Console and received Application ID. 在Google Cast控制台上注册了新的自定义接收器,并收到了应用程序ID。
  2. Created cast_receiver.js file. 创建了cast_receiver.js文件。 The code inside this file is supposed to decode Base64 string into image. 该文件中的代码应该将Base64字符串解码为图像。
  3. Copied the code for Receiver from guide to .js file and changed NAMESPACE inside to my one: urn:x-cast:com.it.innovations.smartbus 将Receiver的代码从指南复制到.js文件,并将里面的NAMESPACE更改为我的代码: urn:x-cast:com.it.innovations.smartbus
  4. Uploaded file on Google Drive and modified its access visibility to full public 在Google云端硬盘上上传了文件,并将其访问权限修改为完全公开
  5. Copied the link to file to URL field in Cast Console. 在Cast控制台中复制了指向文件到URL的链接 This link is direct download of file. 该链接是文件的直接下载。
  6. Restarted Chromecast. 重新启动Chromecast。 Seems like it tried to download something but not sure if succeed 似乎它尝试下载某些内容,但不确定是否成功

If anyone faced this problem, please point me out what I am doing wrong. 如果有人遇到此问题,请指出我做错了什么。 Appreciate any help. 感谢任何帮助。

PS tell if some more code needed... PS告诉是否需要更多代码...

I very strongly suggest to avoid using the sendMessage() for sending any large data set; 强烈建议避免使用sendMessage()发送任何大数据集。 those channels are meant to be used as control channels and not as a way to send a chunk of data. 这些通道应被用作控制通道,而不是作为发送数据块的方式。 A much much simpler and more robust approach is to embed a tiny dumb web server in your local app (on the sender side) and "serve" your images to your chromecast. 一种更简单,更可靠的方法是在本地应用程序中(发送方)嵌入一个小型的Web服务器,然后将图像“提供”给chromecast。 There is a number of ready-to-use embedded web servers that you can put in your app and requires almost no configuration; 您可以在应用程序中放入许多现成的嵌入式Web服务器,并且几乎不需要进行任何配置。 then you can serve all sorts of media, including images, to your chromecast with even the default or styled receiver. 那么您甚至可以使用默认或样式化的接收器将各种媒体(包括图像)投放到chromecast。

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

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