Android:使用PNG格式的Bitmap.compress质量较差 [英] Android: Bitmap.compress poor quality with PNG format

查看:751
本文介绍了Android:使用PNG格式的Bitmap.compress质量较差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序捕获了录像并将其另存为.mp4文件.我想从该视频中提取一帧并将其保存到文件中.由于没有更好的发现,因此我决定使用MediaMetadataRetriever.getFrameAtTime().它发生在从AsyncTask继承的类内部.这是我的代码看起来像我的doInBackground():

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

saveBitmap()方法:

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

现在,问题在于,导出图像的质量明显低于视频质量.我认为PNG输出格式不应该发生这种情况.您可以看到以下区别:

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:

第一张图片是使用台式机上的ffmpeg从视频中提取的.第二个是使用上面的代码在Samsung Galaxy S6上提取的.在我使用的每台Android设备上,结果看起来都差不多.

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?

推荐答案

我找到了解决此问题的其他方法.您可以使用 bigflake的示例构建提取视频帧的机制.您必须添加的一件事是寻求机制.这样效果很好,可以保持确切的质量,并且不需要任何第三方库.到目前为止,我仅注意到的缺点是,与原始想法相比,这将导致更长的执行时间.

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.

这篇关于Android:使用PNG格式的Bitmap.compress质量较差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆