toDataURL中的默认图像质量是多少?

时间:2015-11-24 21:27:32

标签: html5 todataurl

以下是toDataURL的{​​{3}}:

Syntax

canvas.toDataURL(type, encoderOptions);

Parameters

type Optional
    A DOMString indicating the image format. The default type is image/png.

encoderOptions Optional
    A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
    If this argument is anything else, the default value for image quality is used. Other arguments are ignored. 

If this argument is anything else, the default value for image quality is used. Other arguments are ignored.。图像质量的默认值是什么?

1 个答案:

答案 0 :(得分:1)

闪烁:JPEG为0.92,WEBP为0.80。

Gecko:JPEG为0.92(无网页支持)

我是如何找到的

我搜索了#34; toDataURL"使用https://code.google.com/p/chromium/codesearch

相关代码 https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/core/html/HTMLCanvasElement.cpp&q=toDataURL&sq=package:chromium&l=517

看起来它设置的质量如此double quality = UndefinedQualityValue;,其中UndefinedQualityValue;是-1.0的常量。

在进一步向下调用链后,我们转到https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/platform/graphics/ImageBuffer.cpp&sq=package:chromium&l=348&rcl=1448382621

bool ImageDataBuffer::encodeImage(const String& mimeType, const double& quality, Vector<unsigned char>* encodedImage) const
{
    if (mimeType == "image/jpeg") {
        int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality;
        if (quality >= 0.0 && quality <= 1.0)
            compressionQuality = static_cast<int>(quality * 100 + 0.5);
        if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage))
            return false;
    } else if (mimeType == "image/webp") {
        int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality;
        if (quality >= 0.0 && quality <= 1.0)
            compressionQuality = static_cast<int>(quality * 100 + 0.5);
        if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage))
            return false;
    } else {
        if (!PNGImageEncoder::encode(*this, encodedImage))
            return false;
        ASSERT(mimeType == "image/png");
    }

    return true;
}

值如下:

  • JPEGImageEncoder::DefaultCompressionQuality = 92 source
  • WEBPImageEncoder::DefaultCompressionQuality = 80 source

壁虎

我们可以按照与上述类似的流程进行mozilla代码搜索,或者只是google&#34; gecko image encoder&#34;它会将您带到以下值: JPEG:0.92 source