【问题标题】:Android screenshot error: RGBA override BLOB format buffer should have height == widthAndroid截图错误:RGBA覆盖BLOB格式缓冲区应该有高度==宽度
【发布时间】:2020-12-04 06:38:09
【问题描述】:

我正在尝试使用服务截取我的 Android 设备(Samsung Galaxy Tab S5e)的屏幕截图。 我正在使用来自here 的代码。

在链接的代码中,使用了ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);。注意PixelFormat.RGBA_8888。现在,这不允许我编译我的项目,我收到错误:Error: Must be one of: ImageFormat.UNKNOWN, ImageFormat.RGB_565... etc

所以我尝试将 PixelFormat.RGBA_8888 更改为 ImageFormat.JPEG 并编译。但是,我的应用程序现在崩溃并显示以下消息:

RGBA 覆盖 BLOB 格式缓冲区应具有高度 == 宽度

我尝试将PixelFormat.RGBA_8888 更改为0x40x1ImageFormat.RGB_565 和其他一些。这样做通常会导致消息异常:

生产者输出缓冲区格式 0x1 与 ImageReader 配置的缓冲区格式 0x4 不匹配。

这与onImageAvailable(ImageReader reader)中描述的格式有某种联系。

我看过following SO post,似乎正确的格式是特定于设备的,但我都试过了,错误是上述之一。

我完全不知所措(而且我是 Java/Android 新手),所以真的需要一些帮助。

【问题讨论】:

标签: android image image-formats pixelformat image-reader


【解决方案1】:

如下创建图片阅读器,

 ImageReader imageReader =ImageReader.newInstance(width,
            height,
            PixelFormat.RGBA_8888,
            MAX_IMAGE_COUNT);

然后在图像可用的监听器上创建位图为,

          try {
                image = reader.acquireLatestImage();
                if (image != null && mImagesProduced == 0){
                    Image.Plane[] planes = image.getPlanes();
                    Buffer imageBuffer = planes[0].getBuffer().rewind();

                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * width;

                    // create bitmap
                    bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height,
                            Bitmap.Config.ARGB_8888);
                    bitmap.copyPixelsFromBuffer(imageBuffer);

                    saveImage(context, bitmap);
                }

希望有效果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多