当前位置:   article > 正文

Java实现图片指定区域裁剪_java根据像素点位置截图

java根据像素点位置截图

一 概述

        待续。

二 代码实例

        Java实现:

  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. public class Image {
  6. public static void main(String[] args) {
  7. try {
  8. //注意image要比mask小
  9. BufferedImage image = ImageIO.read(Image.class.getResource("../resources/350h.jpg"));
  10. BufferedImage mask = ImageIO.read(Image.class.getResource("../resources/40.jpg"));
  11. File file = new File("C:/Users/windows/Desktop/test7.png");
  12. int offsetX = mask.getWidth();
  13. int offsetY = mask.getHeight();
  14. BufferedImage mixedImage = maskImage(image, mask, 100, 100);
  15. ImageIO.write(mixedImage, "png", file);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. public static BufferedImage maskImage(BufferedImage image, BufferedImage mask, Integer offsetX, Integer offsetY) {
  21. int width = image.getWidth();
  22. int height = image.getHeight();
  23. // 关键在于这里新建的图片类型一定是TYPE_INT_ARGB的
  24. BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  25. int index = 0;
  26. for (int i = 0; i < width; i++) {
  27. for (int j = 0; j < height; j++) {
  28. index++;
  29. System.out.println("第" + index + "个点");
  30. // result[i][j] = bufImg.getRGB(i, j); int rgb = bufImg.getRGB(i, j);
  31. int pixel = image.getRGB(i, j);
  32. Color color = new Color(pixel);
  33. int r = color.getRed();
  34. int g = color.getGreen();
  35. int b = color.getBlue();
  36. System.out.println("横坐标为:" + i + ";纵坐标为:" + j + "的像素为值为" + pixel);
  37. System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
  38. int alpha = (pixel >> 24) & 0xff;
  39. color = new Color(alpha);
  40. r = color.getRed();
  41. g = color.getGreen();
  42. b = color.getBlue();
  43. System.out.println("横坐标为:" + i + ";纵坐标为:" + j + "的像素为值为" + alpha);
  44. System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
  45. int maskPixel = mask.getRGB(offsetX + i, offsetY + j);
  46. color = new Color(maskPixel);
  47. r = color.getRed();
  48. g = color.getGreen();
  49. b = color.getBlue();
  50. int x = offsetX + i;
  51. int y = offsetY + j;
  52. System.out.println("横坐标为:" + x + ";纵坐标为:" + y + "的像素为值为" + maskPixel);
  53. System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
  54. int newPixel = alpha == 0 ? pixel : maskPixel;
  55. color = new Color(newPixel);
  56. r = color.getRed();
  57. g = color.getGreen();
  58. b = color.getBlue();
  59. System.out.println("横坐标为:" + x + ";纵坐标为:" + y + "的新像素为值为" + newPixel);
  60. System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
  61. System.out.println("################################################################");
  62. newImage.setRGB(i, j, newPixel);
  63. }
  64. }
  65. return newImage;
  66. }
  67. }

        解析待续。。。

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

闽ICP备14008679号