Android: Bitmap.compress poor quality with PNG format

寵の児 提交于 2019-12-23 01:35:14

问题


My application captures video footage and saves it as .mp4 file. I would like to extract one frame from this video and also save it to file. Since I haven't found nothing better, I've decided to use MediaMetadataRetriever.getFrameAtTime() for that. It happens inside the class that inherits from AsyncTask. Here is how my code looks like my doInBackground():

Bitmap bitmap1 = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
    retriever.setDataSource(src);
    bitmap1 = retriever.getFrameAtTime(timeUs, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);

    if (Utils.saveBitmap(bitmap1, dst)) {
        Log.d(TAG, "doInBackground Image export OK");
    } else {
        Log.d(TAG, "doInBackground Image export FAILED");
    }
} catch (RuntimeException ex) {
    Log.d(TAG, "doInBackground Image export FAILED");
} finally {
    retriever.release();
    if (bitmap1 != null) {
        bitmap1.recycle();
    }
}

And the saveBitmap() method:

File file = new File(filepath);
boolean result;
try {
    result = file.createNewFile();

    if (!result) {
        return false;
    }

    FileOutputStream ostream = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
    ostream.close();
    return true;
} catch (Exception e) {
    e.printStackTrace();
    return false;
}

Now, the problem is that the quality of the exported image is noticeably worse then the video quality. I don't think that this should happen with PNG output format. You can see the difference below:

The first image was extracted from the video using ffmpeg on my desktop. The second one was extracted using the code above on Samsung Galaxy S6. The result looks pretty much the same on every Android device I was using.

Can someone tell how can I improve the quality of the exported picture?


回答1:


I found other solution for the issue. You can use bigflake's example to build mechanism for extracting video frame. The one thing you will have to add is seeking mechanism. This works well, keeps the exact quality and does not require any third-party libraries. Only downside I've noticed so far is that it will result in longer execution time than the original idea.



来源:https://stackoverflow.com/questions/31789609/android-bitmap-compress-poor-quality-with-png-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!