简体   繁体   English

如何使用javascript将图像从PNG转换为JPEG?

[英]How to convert a image from PNG to JPEG using javascript?

I am trying to get image from canvas.我正在尝试从画布中获取图像。在此处输入图片说明 PNG image is coming properly but the JPEG is producing a problem. PNG 图像正常,但 JPEG 产生问题。 I attached image here.First image is my canvas.Followed by PNG then JPEG.So i want my JPEG image with white background or i need a solution for PNG to JPEG conversion in JS我在这里附上了图片。第一张图片是我的画布。接着是 PNG 然后是 JPEG。所以我想要我的 JPEG 图像有白色背景,或者我需要一个解决方案,在 JS 中将 PNG 转换为 JPEG

canvas =  $('.jSignature')[0];

            imgData = canvas.toDataURL();
            imgDatajpeg = canvas.toDataURL("image/jpeg");                   

            $(".sigCapHolder").append('<img src='+imgData+' /> ')
            $(".sigCapHolder").append('<img src='+imgDatajpeg+' /> ')

Cause原因

The reason for this to happen is due to canvas being transparent.发生这种情况的原因是画布是透明的。 However the transparancy color is black with a transparent alpha-channel so it won't show on screen.然而,透明颜色是黑色的,带有透明的 alpha 通道,所以它不会显示在屏幕上。

JPEG on the other side doesn't support alpha-channel so that black color which is the default is stripped of its alpha channel leaving a black background.另一方面,JPEG 不支持 alpha 通道,因此默认的黑色会从其 alpha 通道中剥离,留下黑色背景。

You PNG supports alpha-channel so it is compatible with the way canvas work.您的 PNG 支持 alpha 通道,因此它与画布的工作方式兼容。

Solution解决方案

To get around this you will have to manually draw in a white background on the canvas before you draw in the image:为了解决这个问题,您必须绘制图像之前在画布上手动绘制白色背景:

var canvas =  $('.jSignature')[0];
var ctx = canvas.getContext('2d');

ctx.fillStyle = '#fff';  /// set white fill style
ctx.fillRect(0, 0, canvas.width, canvas.height);

/// draw image and then use toDataURL() here

Just as an alternative way, using a package to convert black background of transparent image to white or any other other based on the provided HEX value, in our作为另一种方式,使用包将透明图像的黑色背景转换为白色或任何其他基于提供的 HEX 值的其他方法,在我们的

const Jimp = require("jimp");

// Read the PNG file and convert it to editable format
Jimp.read("./images/your-image.png", function (err, image) {
    if (err) {
        // Return if any error
        console.log(err);
        return;
    }

    image.background(0xFFFFFFFF, (err, val) => {
        // Convert image to JPG and store it to 
        // './output/' folder with 'out.jpg' name
        image.write("./output/out.jpg");
    })

});

这适用于火狐,oCanvas.toDataURL("image/jpeg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php和javascript将多个div转换为图像(png/jpeg)? - how to convert multiple div into image (png/jpeg) using php and javascript? 如何使用JavaScript将jpg图像转换为png - How to convert jpg image to png using javascript 如何使用Javascript将原始图像字节数组转换为PNG图像 - How to convert the raw image byte array to PNG image using Javascript 如何在 Javascript 中设置 JPEG/PNG 图像的分辨率/密度? - How to set a resolution/density in JPEG/PNG image in Javascript? 如何在 JavaScript 中将 jpeg 转换为逐行图像? - How to convert jpeg to progressive image in JavaScript? 如何在jpeg或png格式的字节数组中在画布上绘制图像 - How to Draw an image on canvas from a byte array in jpeg or png format 如何在JavaScript中将字符串png转换为图像 - how to convert string png to image in javascript 如何将SVG图像转换为PNG或JPEG等标准图像格式? - How do I convert an SVG image to a standard image format such as PNG or JPEG? 使用 Javascript 将图像流 (JFIF - JPEG) 格式转换为 DataURI - Convert Image Stream (JFIF - JPEG) format to DataURI using Javascript JavaScript:如何将jpeg图像标签转换为原始二进制文件? - JavaScript:How to convert jpeg image tag to raw binary file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM