赞
踩
待续。
Java实现:
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
-
- public class Image {
-
- public static void main(String[] args) {
- try {
- //注意image要比mask小
- BufferedImage image = ImageIO.read(Image.class.getResource("../resources/350h.jpg"));
- BufferedImage mask = ImageIO.read(Image.class.getResource("../resources/40.jpg"));
-
- File file = new File("C:/Users/windows/Desktop/test7.png");
-
- int offsetX = mask.getWidth();
- int offsetY = mask.getHeight();
- BufferedImage mixedImage = maskImage(image, mask, 100, 100);
- ImageIO.write(mixedImage, "png", file);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static BufferedImage maskImage(BufferedImage image, BufferedImage mask, Integer offsetX, Integer offsetY) {
-
- int width = image.getWidth();
- int height = image.getHeight();
-
- // 关键在于这里新建的图片类型一定是TYPE_INT_ARGB的
- BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
-
- int index = 0;
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- index++;
- System.out.println("第" + index + "个点");
- // result[i][j] = bufImg.getRGB(i, j); int rgb = bufImg.getRGB(i, j);
- int pixel = image.getRGB(i, j);
- Color color = new Color(pixel);
- int r = color.getRed();
- int g = color.getGreen();
- int b = color.getBlue();
- System.out.println("横坐标为:" + i + ";纵坐标为:" + j + "的像素为值为" + pixel);
- System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
- int alpha = (pixel >> 24) & 0xff;
- color = new Color(alpha);
- r = color.getRed();
- g = color.getGreen();
- b = color.getBlue();
- System.out.println("横坐标为:" + i + ";纵坐标为:" + j + "的像素为值为" + alpha);
- System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
- int maskPixel = mask.getRGB(offsetX + i, offsetY + j);
- color = new Color(maskPixel);
- r = color.getRed();
- g = color.getGreen();
- b = color.getBlue();
- int x = offsetX + i;
- int y = offsetY + j;
- System.out.println("横坐标为:" + x + ";纵坐标为:" + y + "的像素为值为" + maskPixel);
- System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
- int newPixel = alpha == 0 ? pixel : maskPixel;
- color = new Color(newPixel);
- r = color.getRed();
- g = color.getGreen();
- b = color.getBlue();
- System.out.println("横坐标为:" + x + ";纵坐标为:" + y + "的新像素为值为" + newPixel);
- System.out.println("\t红=" + r + "\t绿" + g + "\t蓝" + b);
- System.out.println("################################################################");
- newImage.setRGB(i, j, newPixel);
- }
- }
- return newImage;
- }
- }
解析待续。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。