Java 图片处理 白底黑字

原图:

效果:

代码:

public static void main(String[] args) throws IOException {
       BufferedImage image = ImageIO.read(new FileInputStream("C:\\Users\\新建文件夹\\2925.jpg"));
       BufferedImage images=replaceWithWhiteColor(image);
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       ImageIO.write(images,"png",out);
       byte[] bb = out.toByteArray();
      
       // 获取当前时间戳设置为文件名避免文件名重复
       SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
       String date = df.format(new Date())
       //存放图片地址及文件名
       String img_dz="C:\\"+date+".png";
       BufferedWriter bw=new BufferedWriter(new FileWriter(img_dz));
       File files = new File(img_dz);
       FileOutputStream fops = new FileOutputStream(files);
       fops.write(bb);
       fops.flush();
       fops.close();
   }

    //处理图片杂色
    public static BufferedImage replaceWithWhiteColor(BufferedImage bi) {

        int[] rgb = new int[3];
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();

        /*
         * 遍历图片的像素
         */

        int hitCount = 0;

        for (int i = minx; i < width-1; i++)
            for (int j = miny; j < height; j++) {
                int pixel = bi.getRGB(i, j);

                int pixelNext = bi.getRGB(i + 1, j);

                rgb[0] = (pixel & 0xff0000) >> 16;

                rgb[1] = (pixel & 0xff00) >> 8;

                rgb[2] = (pixel & 0xff);

               if(rgb[0] > 105){//白色
                   bi.setRGB(i, j,  0xffffff);
               }else if(rgb[0] > 106){
                   bi.setRGB(i, j,  0x555555);;
               }else if(rgb[1] > 75){
                   bi.setRGB(i, j,  0x000000);
               }else if(rgb[2] > 0){
                   bi.setRGB(i, j,  0x000000);

               }else{//黑色
                   bi.setRGB(i, j,  0x000000);
               }


            }

        return bi;

    }

如果你觉得这篇内容对你挺有启发请点赞+关注

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值