简体   繁体   English

HTML5 Canvas图像裁剪,调整大小和保存而不会降低质量

[英]HTML5 Canvas image crop, resize, and save without losing quality

http://i.stack.imgur.com/A3p9c.png

  • I have canvas with width equal to window width. 我的画布宽度等于窗口宽度。
  • I am drawing an image(1920x1080) to canvas. 我正在将图像(1920x1080)绘制到画布上。
  • Then I selected area to crop image inside canvas which will be 492x369. 然后,我选择了要在画布内裁剪图像的区域,该区域将为492x369。
  • After cropping image I want to scale that cropped image to 1024x768. 裁剪图像后,我想将该裁剪的图像缩放到1024x768。

But when I scale that image it loses its quality.. 但是当我缩放该图像时,它会失去质量。

How can I scale image without losing its quality? 如何缩放图像而又不损失其质量?

The steps which I performed are 我执行的步骤是

  1. Added Image to Main Canvas 将图像添加到主画布
  2. Selected Crop area on Canvas 画布上的选定作物区域
  3. using getImageData() stored croped image data in temp variable. 使用getImageData()将裁剪的图像数据存储在temp变量中。
  4. created new canvas in memory 在内存中创建了新画布
  5. using putImageData() added image data to new in memory canvas 使用putImageData()将图像数据添加到内存画布中的新图像
  6. then getting base64 image data using newcanvas.toDataURL() 然后使用newcanvas.toDataURL()获取base64图像数据

The simple answer is that you can't.. 简单的答案是你不能..

The image, when scaled, is interpolated. 缩放后对图像进行插值。 If the source image is of low quality (as in this video snapshot) the result will be at best equal, but never better without some complex processing. 如果源图像的质量较低(如此视频快照中所示),则结果最多将是相等的,但如果不进行某些复杂的处理,则永远不会更好。

In this case the video snapshot is heavily compressed so you get a blocky image (a result of MPEG compression) which is not so apparent at the original size but becomes evident when scaled (you just see the artifacts better as they too are scaled). 在这种情况下,视频快照会被大量压缩,因此您会得到一个块状图像(MPEG压缩的结果),该图像在原始大小下不那么明显,但是在缩放后变得明显(您可以更好地看到这些伪像,因为它们也在缩放)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM