public class ClearImageHelper {
public static BufferedImage cleanImage(BufferedImage bufferedImage)throws IOException{  
        int h = bufferedImage.getHeight();  
        int w = bufferedImage.getWidth();  
  
        // 灰度化  
        int[][] gray = new int[w][h];  
        for (int x = 0; x < w; x++){  
            for (int y = 0; y < h; y++){  
                int argb = bufferedImage.getRGB(x, y);  
                // 图像加亮(调整亮度识别率非常高)  
                int r = (int) (((argb >> 16) & 0xFF) * 1.1 + 30);  
                int g = (int) (((argb >> 8) & 0xFF) * 1.1 + 30);  
                int b = (int) (((argb >> 0) & 0xFF) * 1.1 + 30);  
                if (r >= 255){  
                    r = 255;  
                }  
                if (g >= 255){  
                    g = 255;  
                }  
                if (b >= 255){  
                    b = 255;  
                }  
                gray[x][y] = (int) Math.pow((Math.pow(r, 2.2) * 0.2973 + Math.pow(g, 2.2)* 0.6274 + Math.pow(b, 2.2) * 0.0753), 1 / 2.2);  
            }  
        }  
  
        // 二值化  
        int threshold = ostu(gray, w, h);  
        BufferedImage binaryBufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_BYTE_BINARY);  
        for (int x = 0; x < w; x++){  
            for (int y = 0; y < h; y++){  
                if (gray[x][y] > threshold){  
                    gray[x][y] |= 0x00FFFF;  
                } else{  
                    gray[x][y] &= 0xFF0000;  
                }  
                binaryBufferedImage.setRGB(x, y, gray[x][y]);  
            }  
        }  
  
        return binaryBufferedImage;
    }  
public static int ostu(int[][] gray, int w, int h){  
        int[] histData = new int[w * h];  
        // Calculate histogram  
        for (int x = 0; x < w; x++){  
            for (int y = 0; y < h; y++){  
                int red = 0xFF & gray[x][y];  
                histData[red]++;  
            }  
        }  
  
        // Total number of pixels  
        int total = w * h;  
  
        float sum = 0;  
        for (int t = 0; t < 256; t++)  
            sum += t * histData[t];  
  
        float sumB = 0;  
        int wB = 0;  
        int wF = 0;  
  
        float varMax = 0;  
        int threshold = 0;  
  
        for (int t = 0; t < 256; t++){  
            wB += histData[t]; // Weight Background  
            if (wB == 0)  
                continue;  
  
            wF = total - wB; // Weight Foreground  
            if (wF == 0)  
                break;  
  
            sumB += (float) (t * histData[t]);  
  
            float mB = sumB / wB; // Mean Background  
            float mF = (sum - sumB) / wF; // Mean Foreground  
  
            // Calculate Between Class Variance  
            float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);  
  
            // Check if new maximum found  
            if (varBetween > varMax){  
                varMax = varBetween;  
                threshold = t;  
            }  
        }  
  
        return threshold;  
    }  
    //图片灰度,黑白  
    public static BufferedImage gray(BufferedImage src) {  
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
    ColorConvertOp op = new ColorConvertOp(cs, null);  
    src = op.filter(src, null);  
        return src;
    }  
    /** 
    * 设置源图片为背景透明,并设置透明度 
    * @param srcImageFile 源图片 
    * @param alpha 透明度 (0-10依次不透明)
    */  
public static BufferedImage transparentImage(String  srcImageFile,int alpha) {  
    BufferedImage bufferedImage=null;
    try {  
    //读取图片  
    FileInputStream stream = new FileInputStream(new File(srcImageFile));// 指定要读取的图片  
    // 定义一个字节数组输出流,用于转换数组  
    ByteArrayOutputStream os = new ByteArrayOutputStream();  
   
    byte[] data =new byte[1024];// 定义一个1K大小的数组  
    while (stream.read(data) != -1) {  
    os.write(data);  
    }  
  
        ImageIcon imageIcon = new ImageIcon(os.toByteArray());  
        bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),  
        BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();  
        g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());  
  
        //判读透明度是否越界  
        if (alpha < 0) {
        alpha = 0;
        } else if (alpha > 10) {
        alpha = 10;
        }
        int c = bufferedImage.getRGB(3, 3);
        // 循环每一个像素点,改变像素点的Alpha值
        for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
        for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
        int rgb = bufferedImage.getRGB(j2, j1);
        if(c==rgb){
        rgb = rgb & 0x00ffffff;
        }else{
        rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);  
        }
        bufferedImage.setRGB(j2, j1, rgb);  
        }  
        }  
        g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());  
  
    } catch (Exception e) {
    e.printStackTrace();
    }finally {
return bufferedImage;
}
    }  
    public static String rootUrl = System.getProperty("user.dir")+File.separator;
    public static String FILE_DIR="C:\\Users\\wzk\\Desktop\\"; 
    public static void main(String[] args) throws IOException{  
        File testDataDir = new File(FILE_DIR+"1.png");//去噪  
        BufferedImage textImage =ImageIO.read(new FileInputStream(testDataDir));
//        cleanImage(textImage);  
        BufferedImage gray = gray(textImage);//灰度化
        ImageIO.write(gray, "png", new File(FILE_DIR+"2.png"));
    }  
}

Java图片透明化操作相关推荐

  1. Java 图片 基础操作

    文章目录 图片读写 读 写 小总结 像素操作 属性操作 缩放 格式转换 其他 统一缩放每个像素的值 执行仿射变换 执行卷积 获取支持读取的文件格式 获取支持写入的文件格式 参考 图片读写 读 impo ...

  2. java使用poi操作word模板,插入图片、段落、表格

    java使用poi操作word插入图片.段落.表格 其他链接 准备工作 创建word模板.docx文件 编写模板格式.xml文件 java上手poi maven依赖 使用到的包 具体应用 对应封装方法 ...

  3. Java图片操作 --- 图片的读取、绘制、缩放、裁剪、保存

    本文链接: http://blog.csdn.net/xietansheng/article/details/78453570 Java Swing 图形界面开发(目录) 1. 读取图片 方法一: 通 ...

  4. java 图片操作_Java对图片的操作

    有一天我在做一个游戏的时候听说可以把图片翻转过来可以减少自己做图片的时间  所以我就在网上找了一下  发现很多文章写得特别复杂 并且有一些方法都不能在eclipse上面用,所以我就模仿网上的文章写出适 ...

  5. Java 使用itextPdf7操作pdf,写入照片这一篇就够了

    Java 使用itextPdf7操作pdf,写入照片这一篇就够了 1. 效果图 1.1 M*N列图片(无边界&有边界) 1.2 图片重叠 1.3 文字背景图片 1.4 图片与文字相邻 & ...

  6. Java 使用 POI 操作 Excel

    Apache POI 基本介绍 Apache POI 是 Apache 软件基金会提供的 100% 开源库.支持 Excel 库的所有基本功能. 图片来源:易百教程 基本概念 在 POI 中,Work ...

  7. java图片转换pdf_Java实现图片转换PDF文件的示例代码

    最近因为一些事情,需要将一张简单的图片转换为PDF的文件格式,在网上找了一些工具,但是这些工具不是需要注册账号,就是需要下载软件. 而对于只是转换一张图片的情况下,这些操作显然是非常繁琐的,所以作者就 ...

  8. java 图片不失真缩放,ico格式图片转换,透明图层,jar->exe

    Java 图片缩放,转ico格式 Java 在转换图片格式为 ico 时 需要引入 image4j 的依赖,这里分享一个自己写好的并通过exe4j 打包好的exe小程序.因为功能太少所以再引入 thu ...

  9. java 图片水印_JAVA实现图片水印

    一.JAVA图片水印实现原理 1.1.JAVA图片水印实现思路 1.创建缓存图片对象. 2.创建Java绘图工具对象. 3.使用绘图工具对象将原图绘制到缓存图片对象. 4.使用绘图工具将水印(文字/图 ...

最新文章

  1. list保留小数位数
  2. [Google Guava] 1.2-前置条件
  3. 数据源名称和 64 位操作系统
  4. “你们对编程和头发一无所知。”
  5. Flink表转流一例+何时应该使用execute()
  6. 介绍一个好用的Chrome扩展,能高效管理cookie
  7. flume的配置和开启
  8. 《网络规划设计师考试大纲》、《网络规划设计师教程》和《系统架构设计师教程》...
  9. matlab刚度矩阵求逆,ANSYS求结构整体刚度矩阵逆矩阵APDL命令流
  10. C语言实现平衡二叉树
  11. Pr全套视频教程 PR 全套零基础从入门到精通视频教程
  12. C# dataGridView控件单元格底色 dataGridView背景色 背景色调整 Header背景色前景色
  13. Uncaught SyntaxError The requested module ‘node_modules.vitevue.jsv=50ccac76‘ does not provide
  14. 向武 清华大学 计算机,哥哥保送弟弟全市第二 双胞胎如何同时上清华?
  15. 如何更改VS的项目名字
  16. HTML中gt的含义
  17. 【最优估计学习笔记】贝叶斯公式的深入理解
  18. 开启人工智能的大门,引领AI时代
  19. UT单元测试(一)——基础流程总结篇
  20. 【Openfire】网页版的用户注册、登录、修改密码

热门文章

  1. markdown发生HTML渲染组件出错的解决方案
  2. AVC1与H264的差别
  3. python爬虫基础之AJAX页面的抓取
  4. mapbox轨迹动画效果
  5. 传球问题-矩阵快速幂
  6. python3 exec locals()
  7. 电动加热护颈枕芯片-DLTAP703SC
  8. 从键盘输入一个整数 n,计算 1 到 n 之间所有偶数有哪些和其偶数的平均数,并输出。
  9. 计算机考研复试---英文问题
  10. 【python OpenCV3.3 图像处理教程:直线检测、圆检测、对象测量、腐蚀、膨胀等形态学操作、数字验证码识别、人脸检测