Java实现图片指定区域裁剪

这篇博客介绍了如何使用Java进行图像处理,特别是将掩模图像应用到源图像上。通过读取和处理两个图像文件,将掩模图像放置在源图像的指定位置,并根据像素的Alpha通道融合两个图像,生成新的混合图像。代码中详细展示了如何获取和设置像素颜色,以及如何处理Alpha透明度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 概述

        待续。

二 代码实例

        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;
    }
}

        解析待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值