繁体   English   中英

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

[英]Send Base64 encoded image as string to Chromecast

问题是将电话中的本地图像作为编码的Base64字符串发送到Chromecast。 并使用我的自定义接收器对其进行解码。 我正在遵循基于该项目样本的 指南。

我建议问题可能出在:

  1. 自定义接收器不合适(我不太擅长JS)。
  2. Chromecast未加载该Receiver(我不知道如何检查)。
  3. 图片在设备上编码错误或在Chromecast上解码。

您会发现,由于我发送照片时Chromecast状态为:

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

这就是我将图像作为String发送的方式:

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

使用的方法:

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();
    }
}

如果发送过程正确,那么接收方是错误的,并且不知道如何正确解码此消息。 上传它的方式(嗯,至少我认为它上传了...)

  1. 在Google Cast控制台上注册了新的自定义接收器,并收到了应用程序ID。
  2. 创建了cast_receiver.js文件。 该文件中的代码应该将Base64字符串解码为图像。
  3. 将Receiver的代码从指南复制到.js文件,并将里面的NAMESPACE更改为我的代码: urn:x-cast:com.it.innovations.smartbus
  4. 在Google云端硬盘上上传了文件,并将其访问权限修改为完全公开
  5. 在Cast控制台中复制了指向文件到URL的链接 该链接是文件的直接下载。
  6. 重新启动Chromecast。 似乎它尝试下载某些内容,但不确定是否成功

如果有人遇到此问题,请指出我做错了什么。 感谢任何帮助。

PS告诉是否需要更多代码...

强烈建议避免使用sendMessage()发送任何大数据集。 这些通道应被用作控制通道,而不是作为发送数据块的方式。 一种更简单,更可靠的方法是在本地应用程序中(发送方)嵌入一个小型的Web服务器,然后将图像“提供”给chromecast。 您可以在应用程序中放入许多现成的嵌入式Web服务器,并且几乎不需要进行任何配置。 那么您甚至可以使用默认或样式化的接收器将各种媒体(包括图像)投放到chromecast。

暂无
暂无

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

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