Search code examples
androidterminalformatcompressionscreenshot

Compress format and quality of screencap?


I would like to ask you the compress format and the quality of the bitmap file generated with the command:

system/bin/screencap -p $path

The reason I am asking it's because if I take I screenshot with screen cap and I am able to perform OCR correctly without the need to preprocess the image.

If I take a screenshot (the same screen) with the MediaProjection API, instead I am unable to get the same result with OCR, unless I preprocess the image. Hence I suppose the reason must be in a different compression and/or quality.


Solution

  •  if (png) {
            AndroidBitmapInfo info;
            info.format = flinger2bitmapFormat(buffer->getPixelFormat());
            info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
            info.width = buffer->getWidth();
            info.height = buffer->getHeight();
            info.stride = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat());
            int result = AndroidBitmap_compress(&info, static_cast<int32_t>(dataspace), base,
                                                ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd,
                                                [](void* fdPtr, const void* data, size_t size) -> bool {
                                                    int bytesWritten = write(*static_cast<int*>(fdPtr),
                                                                             data, size);
                                                    return bytesWritten == size;
                                                });
            if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
                fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result);
            }
            if (fn != NULL) {
                notifyMediaScanner(fn);
            }
        }
    

    Source: https://android.googlesource.com/platform/frameworks/base/+/master/cmds/screencap/screencap.cpp