当前位置:   article > 正文

js改变图片曝光度(高亮度)

js改变图片曝光度(高亮度)

方法一:

  • 原理:

使用canvas进行滤镜操作,通过改变图片数据每个像素点的RGB值来提高图片亮度。

  • 缺点

当前项目使用的是svg,而不是canvas

调整出来的效果不是很好,图片不是高亮,而是有些发白

  • 效果

  • 代码
  1. <body>
  2. 高亮:<input type="range" id="exposureRange" min="-50" max="50" value="0"><span id="exposureValueBox">0</span>
  3. <br>
  4. <input type="file" id="fileInput" accept="image/*">
  5. <canvas id="canvas"></canvas>
  6. <script>
  7. const exposureRange = document.getElementById('exposureRange');
  8. const fileInput = document.getElementById('fileInput');
  9. const exposureValueBox = document.getElementById('exposureValueBox');
  10. const canvas = document.getElementById('canvas');
  11. const ctx = canvas.getContext('2d');
  12. let originalImageData;
  13. let exposureValue;
  14. exposureRange.addEventListener('input', function (event) {
  15. exposureValue = parseInt(exposureRange.value);
  16. exposureValueBox.innerHTML = exposureValue
  17. highlightImage(exposureValue);
  18. });
  19. fileInput.addEventListener('change', function (event) {
  20. var file = event.target.files[0];
  21. var reader = new FileReader();
  22. reader.onload = function (event) {
  23. var img = new Image();
  24. img.onload = function () {
  25. canvas.width = img.width;
  26. canvas.height = img.height;
  27. ctx.drawImage(img, 0, 0);
  28. originalImageData = img;
  29. }
  30. img.src = event.target.result;
  31. }
  32. reader.readAsDataURL(file);
  33. });
  34. function highlightImage(brightnessValue = 0) {
  35. const img = originalImageData.cloneNode(); // 使用原始图像的副本
  36. const imgCanvas = document.createElement('canvas');
  37. const imgCtx = imgCanvas.getContext('2d');
  38. imgCanvas.width = img.width;
  39. imgCanvas.height = img.height;
  40. imgCtx.drawImage(img, 0, 0);
  41. const imageData = imgCtx.getImageData(0, 0, imgCanvas.width, imgCanvas.height);
  42. const data = imageData.data;
  43. for (let i = 0; i < data.length; i += 4) {
  44. data[i] = clamp(data[i] + brightnessValue, 0, 255); // Red channel
  45. data[i + 1] = clamp(data[i + 1] + brightnessValue, 0, 255); // Green channel
  46. data[i + 2] = clamp(data[i + 2] + brightnessValue, 0, 255); // Blue channel
  47. }
  48. imgCtx.putImageData(imageData, 0, 0);
  49. ctx.clearRect(0, 0, canvas.width, canvas.height);
  50. ctx.drawImage(imgCanvas, 0, 0);
  51. }
  52. function clamp(value, min, max) {
  53. return Math.min(Math.max(value, min), max);
  54. }
  55. </script>
  56. </body>

方法二:

  • 原理

通过使用svg滤镜feColorMatrix(基于转换矩阵对颜色进行变换,每—像素的颜色值都经过矩阵计算出新颜色),改变图片每个像素的颜色值来提高图片亮度。

  • 优点

当前代码使用svg渲染图片,操作对象也是svg;

准确的提高图片亮度,而不是给图片加白;

  • 效果

  • 代码
  1. <body>
  2. <input type="range" id="brightnessRange" min="0" max="2000" value="1000">
  3. <br>
  4. <svg id="svgCanvas" width="450" height="300">
  5. <!-- filter:定义一个svg滤镜容器 id:用于标识滤镜 -->
  6. <filter id="brightnessFilter">
  7. <!-- feColorMatrix:svg滤镜类型 -->
  8. <feColorMatrix type="matrix" values="1 0 0 0 0
  9. 0 1 0 0 0
  10. 0 0 1 0 0
  11. 0 0 0 1 0"/>
  12. </filter>
  13. <image id="image" width="450" height="300" xlink:href="laohu.png" filter="url(#brightnessFilter)"></image>
  14. </svg>
  15. <script>
  16. const brightnessRange = document.getElementById('brightnessRange');
  17. const image = document.getElementById('image');
  18. const filter = document.getElementById('brightnessFilter');
  19. brightnessRange.addEventListener('input', adjustBrightness);
  20. function adjustBrightness() {
  21. const brightnessValue = parseFloat(brightnessRange.value);
  22. applyBrightnessAdjustment(brightnessValue);
  23. }
  24. function applyBrightnessAdjustment(brightnessValue) {
  25. const matrixValues = [
  26. brightnessValue / 100, 0, 0, 0, 0,
  27. 0, brightnessValue / 100, 0, 0, 0,
  28. 0, 0, brightnessValue / 100, 0, 0,
  29. 0, 0, 0, 1, 0
  30. ];
  31. filter.querySelector('feColorMatrix').setAttribute('values', matrixValues.join(' '));
  32. }
  33. </script>
  34. </body>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/328666
推荐阅读
相关标签
  

闽ICP备14008679号