LumAdjust.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.fire;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6. public class LumAdjust {
  7. /**
  8. * 图片亮度调整
  9. * @param image
  10. * @param param
  11. * @throws IOException
  12. */
  13. public void lumAdjustment(BufferedImage image, int param) throws IOException {
  14. if (image == null) {
  15. return;
  16. } else {
  17. int rgb, R, G, B;
  18. for (int i = 0; i < image.getWidth(); i++) {
  19. for (int j = 0; j < image.getHeight(); j++) {
  20. rgb = image.getRGB(i, j);
  21. R = ((rgb >> 16) & 0xff) + param;
  22. G = ((rgb >> 8) & 0xff) + param;
  23. B = (rgb & 0xff) + param;
  24. rgb = ((clamp(255) & 0xff) << 24) | ((clamp(R) & 0xff) << 16) | ((clamp(G) & 0xff) << 8)
  25. | ((clamp(B) & 0xff));
  26. image.setRGB(i, j, rgb);
  27. }
  28. }
  29. }
  30. }
  31. // 判断a,r,g,b值,大于256返回256,小于0则返回0,0到256之间则直接返回原始值
  32. private int clamp(int rgb) {
  33. if (rgb > 255)
  34. return 255;
  35. if (rgb < 0)
  36. return 0;
  37. return rgb;
  38. }
  39. public static void main(String[] args) throws IOException {
  40. System.out.println("aaaaaaaaaaaa");
  41. File file = new File("D://red.jpg");
  42. BufferedImage image = ImageIO.read(file);
  43. LumAdjust lumAdjust = new LumAdjust();
  44. lumAdjust.lumAdjustment(image, -125);
  45. File file2 = new File("D://2.jpg");
  46. file2.createNewFile();
  47. ImageIO.write(image, "JPG", file2);
  48. }
  49. }