java 图片添加蒙版处理

参考网站:http://www.jhlabs.com/ip/filters/index.html
修改后的jar包与未编译文件,以及详细解说:
https://download.youkuaiyun.com/download/tjj3027/10327417

为指定图片添加指定颜色蒙版,实现图片添加蒙版的代码:

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;


import com.jhlabs.image.ContrastFilter;
import com.jhlabs.image.GrayscaleFilter;
import com.jhlabs.image.MaskFilter;


public class ChangeImageColor {
      public static final int redmask = 0xffff0000;//红色
      public static final int graymask = 0x00cecece;//灰色
      public static final int orangemask = 0x0000ff00;//橘色
      public static final int bluemask = 0x000000ff;//蓝色
      public static final int alphamask = 0xaa000000;
        /**
         * 为指定图片添加指定颜色蒙版
         * @param initPath        原始图片路径:路径+图片名称+图片后缀
         * @param savePath            添加蒙版后图片保存路径:路径+图片名称+图片后缀
         * @param mask            为指定图片添加蒙版的颜色及透明度共32 位,0x表示16进制  三四位表示蒙版透明度 后六位为颜色值 
         */
        public void setBaseImg(String initPath,String savePath,int mask){
            File file=new File(initPath);
            BufferedImage srcImg=null;//原始图片
            BufferedImage destImg=null;//生成目标图片
            try{
                srcImg=ImageIO.read(file);
                int width=srcImg.getWidth();
                int height=srcImg.getHeight();
                int minx=srcImg.getMinX();//通常为0
                int miny=srcImg.getMinY();
                MaskFilter maskfilter=new MaskFilter();
                ContrastFilter contrastfilter=new ContrastFilter();
                GrayscaleFilter grayscalefilter=new GrayscaleFilter();
                //新建一个BufferedImage 防止某些PNG图片无法修改
                destImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                //循环每个像素
                for (int i = minx; i < width; i++) {  
                    for (int j = miny; j < height; j++) {  
                        int pixel = srcImg.getRGB(i, j);//rgb值
                        //灰度处理后rgb的值
                        //需要灰度处理否则直接添加蒙版 因为不确定图片上某点的颜色值 若颜色相同,叠加后,无法得到想要的蒙版色值效果
                        int rgbFilter= grayscalefilter.filterRGB(i, j, pixel);;
                        destImg.setRGB(i, j, rgbFilter);
                    }
                } 
                //灰度处理后降低对比度,使得图片添加蒙版后可以清晰的看到原图的轮廓
                //设置对比度
                contrastfilter.setContrast(2f);
                //设置亮度
                contrastfilter.setBrightness(0.9f);
                //根据对比度 亮度得到处理后的图片bi,必须执行否则亮度与对比度设置的值不起作用
                srcImg=contrastfilter.filter(destImg,srcImg);
                //添加蒙版
                for (int i = minx; i < width; i++) {  //循环每个像素 然后修改
                    for (int j = miny; j < height; j++) {  
                        int rgbFilter= srcImg.getRGB(i, j);;
                        if (mask == redmask) {
                            maskfilter.setMask(0xffff0000);
                            rgbFilter = maskfilter.filterRGB(i, j, rgbFilter);
                        } else if (mask == graymask) {
                            maskfilter.setMask(0xffffffff);
                            rgbFilter = maskfilter.filterRGB(i, j, rgbFilter);
                        } else if (mask == orangemask) {
                            maskfilter.setMask(0xffce7e1b);
                            rgbFilter = maskfilter.filterRGB(i, j, rgbFilter);
                        } else if (mask == bluemask) {
                            maskfilter.setMask(0xff38cdd3);
                            rgbFilter = maskfilter.filterRGB(i, j, rgbFilter);
                        }
                        destImg.setRGB(i, j, rgbFilter);
                    }
                } 
                //输出图片
                ImageIO.write(destImg, "png", new File(savePath));  
            }catch(Exception ex){
                System.out.println(ex.toString());
            }
            srcImg.flush();
            destImg.flush();
        }

        public static void main(String[] args) {
            ChangeImageColor change=new ChangeImageColor();
            String path = "D:\\0.png";
            String savePath1 = "D:\\1.png";
            String savePath2 = "D:\\2.png";
            String savePath3 = "D:\\3.png";
            String savePath4 = "D:\\4.png";
            //红色蒙版
            change.setBaseImg(path,savePath1,redmask);
            //灰色蒙版
            change.setBaseImg(path,savePath2,graymask);
            //橘色蒙版
            change.setBaseImg(path,savePath3,orangemask);
            //蓝色蒙版
            change.setBaseImg(path,savePath4,bluemask);
        }
}

引入jar中文件:com.jhlabs.image.ContrastFilter报空指针错误,需要简单修改为:

package com.jhlabs.image;

import java.awt.*;
import java.awt.image.*;

/**
 * A filter to change the brightness and contrast of an image.
 */
public class ContrastFilter extends TransferFilter {

    private float brightness = 1.0f;
    private float contrast = 1.0f;

    protected float transferFunction( float f ) {
        f = f*brightness;
        f = (f-0.5f)*contrast+0.5f;
        return f;
    }

    /**
     * Set the filter brightness.
     * @param brightness the brightness in the range 0 to 1
     * @min-value 0
     * @max-value 0
     * @see #getBrightness
     */
    public void setBrightness(float brightness) {
        this.brightness = brightness;
        initialized = false;
    }

    /**
     * Get the filter brightness.
     * @return the brightness in the range 0 to 1
     * @see #setBrightness
     */
    public float getBrightness() {
        return brightness;
    }

    /**
     * Set the filter contrast.
     * @param contrast the contrast in the range 0 to 1
     * @min-value 0
     * @max-value 0
     * @see #getContrast
     */
    public void setContrast(float contrast) {
        this.contrast = contrast;
        initialized = false;
    }

    /**
     * Get the filter contrast.
     * @return the contrast in the range 0 to 1
     * @see #setContrast
     */
    public float getContrast() {
        return contrast;
    }

    @Override
    public BufferedImage filter(BufferedImage src, BufferedImage dest) {
        int width = src.getWidth();
        int height = src.getHeight();

        if (dest == null)
            dest = createCompatibleDestImage(src, null);

        int[] inPixels = new int[width * height];
        int[] outPixels = new int[width * height];
        src.getRGB(0, 0, width, height, inPixels, 0, width);

        // calculate RED, GREEN, BLUE means of pixel
        int index = 0;
        int[] rgbmeans = new int[3];
        double redSum = 0, greenSum = 0, blueSum = 0;
        double total = height * width;
        for (int row = 0; row < height; row++) {
            int ta = 0, tr = 0, tg = 0, tb = 0;
            for (int col = 0; col < width; col++) {
                index = row * width + col;
                ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                redSum += tr;
                greenSum += tg;
                blueSum += tb;
            }
        }

        rgbmeans[0] = (int) (redSum / total);
        rgbmeans[1] = (int) (greenSum / total);
        rgbmeans[2] = (int) (blueSum / total);

        // adjust contrast and brightness algorithm, here
        for (int row = 0; row < height; row++) {
            int ta = 0, tr = 0, tg = 0, tb = 0;
            for (int col = 0; col < width; col++) {
                index = row * width + col;
                ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;

                // remove means
                tr -= rgbmeans[0];
                tg -= rgbmeans[1];
                tb -= rgbmeans[2];

                // adjust contrast now !!!
                tr = (int) (tr * getContrast());
                tg = (int) (tg * getContrast());
                tb = (int) (tb * getContrast());

                // adjust brightness
                tr += (int) (rgbmeans[0] * getBrightness());
                tg += (int) (rgbmeans[1] * getBrightness());
                tb += (int) (rgbmeans[2] * getBrightness());
                outPixels[index] = (ta << 24) | (PixelUtils.clamp(tr) << 16) | (PixelUtils.clamp(tg) << 8) | PixelUtils.clamp(tb);
            }
        }
        setRGB(dest, 0, 0, width, height, outPixels);
        return dest;
    }

    public String toString() {
        return "Colors/Contrast...";
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三知之灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值