java处理图片

package com.aop.utils;


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


import javax.imageio.ImageIO;


public class ImageUtil {

/**
 * 获取图片
 * @param file
 * @return
 */
public  static BufferedImage initBufferedImage(File file) {
       // File file = new File(imagePath);
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
/**
 * 透明反白使用
 * @param sourceImage
 * @return
 */
public  static BufferedImage whiteProcess(BufferedImage sourceImage){
    int width = sourceImage.getWidth();
    int height = sourceImage.getHeight();
    BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);// BufferedImage.TYPE_BYTE_GRAY指定了这是一个灰度图片 
    for(int i= 0 ; i < width ; i++){  
            for(int j = 0 ; j < height; j++){
                int rgb = sourceImage.getRGB(i, j);
                /*if(rgb>0){
                 grayImage.setRGB(i, j, -16382199);
                }else{
                 grayImage.setRGB(i, j, -1); 
                }*/
        }  
    } 
    return grayImage;
}
/**
 * 判断图片是jpg还是png格式,false代表jpg,true代表png
 * @param name
 * @return
 */
public static Boolean imageFormat(String name){
Boolean format=null;
if(name.equalsIgnoreCase("jpg")){
format=false;
}
if(name.equalsIgnoreCase("png")){
format=true;
}
return format;
}

/**
 * 将图片写成文件
 * @param img
 * @param filePath
 * @throws IOException
 */
public static void writeBufferedImage(BufferedImage img,String filePath) throws IOException{
int lastIndexOf = filePath.lastIndexOf(".");
String format = filePath.substring(lastIndexOf+1);
        //获取图片格式
        System.out.println(format);
         ImageIO.write(img,format,new File(filePath));
    }
/**
 * 判断是否透明, true透明
 * @param sourceImage
 * @return
 */
public static Boolean isTransparency(BufferedImage sourceImage){
 int width = sourceImage.getWidth();
    int height = sourceImage.getHeight();
    Boolean flag=true;
    int[] a=new int[width*height];
    int num=0;
    int aphSum=1;
    int sumPh=1;
    //BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);// BufferedImage.TYPE_BYTE_GRAY指定了这是一个灰度图片 
    for(int i= 0 ; i < width ; i++){  
            for(int j = 0 ; j < height; j++){
                int rgb = sourceImage.getRGB(i, j);
                a[num]=rgb;
                num++;
        }  
    } 
    for(int i=0;i<a.length;i++){
     if(a[i]>0){
     aphSum++;
     }else{
     sumPh++;
     }
    }
    if(aphSum<sumPh){
     flag=false;
    }
return flag;
}
  /**
     * 处理透明度
     */
    public BufferedImage alphaProcess(BufferedImage bufferedImage) {
        //获取源图像的宽高
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        //实例化一个同样大小的图片,并将type设为 BufferedImage.TYPE_4BYTE_ABGR,支持alpha通道的rgb图像
        BufferedImage resImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);


        double grayMean = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = bufferedImage.getRGB(i,j);
                int r = (0xff&rgb);
                int g = (0xff&(rgb>>8));
                int b = (0xff&(rgb>>16));
                //这是灰度值的计算公式
                grayMean += (r*0.299+g*0.587+b*0.114);
            }
        }
        //计算平均灰度
        grayMean = grayMean/(width*height);


        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = bufferedImage.getRGB(i,j);
                //一个int是32位,java中按abgr的顺序存储,即前8位是alpha,最后8位是r,所以可以通过下面的方式获取到rgb的值
                int r = (0xff&rgb);
                int g = (0xff&(rgb>>8));
                int b = (0xff&(rgb>>16));
                double gray = (r*0.299+g*0.587+b*0.114);
                //如果灰度值大于之前求的平均灰度值,则将其alpha设为0,下面准确写应该是rgb = r + (g << 8) + (b << 16) + (0 << 24);
                if (gray>grayMean){
                    rgb = r + (g << 8) + (b << 16);
                }
                resImage.setRGB(i,j,rgb);
            }
        }
        //ok,返回的就是将浅色背景设为透明的BufferedImage了,可以用灰度化里提到的方式写成文件
        return resImage;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值