java 二维码扫描报错 NotFoundException

Java图像处理:二值化与解码优化
文章介绍了使用Java进行图像文件读取,包括BufferedImage的处理、二值化、异常处理以及多格式解码。主要关注如何使用ImageOptimizationUtil对图片进行优化,如二值化去除小黑点并尝试硬解码以提高二维码识别成功率。
public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\a.png");
        BufferedImage image = ImageIO.read(file);
        HashMap<DecodeHintType, Object> hints = new HashMap<>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        MultiFormatReader reader = new MultiFormatReader();
        Result result = null;
        try {
            //第一次正常读取
            result = reader.decode(new BinaryBitmap(new HybridBinarizer(
                    new BufferedImageLuminanceSource(image))), hints);
        } catch (NotFoundException e) {
            // 正常读取不到进行二次读取
            e.printStackTrace();
            //二值化后的图像的开运算:先腐蚀再膨胀(用于去除图像的小黑点)
            BufferedImage image1 = ImageOptimizationUtil.opening(image);
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            //设置优化精度
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            //设置复杂模式开启
            hints.put(DecodeHintType.PURE_BARCODE,Boolean.TRUE);
            try {
                result = reader.decode(new BinaryBitmap(new HybridBinarizer(
                        new BufferedImageLuminanceSource(image1))),hints);
            } catch (NotFoundException ex) {
                throw new RuntimeException(ex);
            }
        }

        if (result != null) {
            // 打印解码的结果
            System.out.println(result.getText());
        }
    }
package com.ruoyi.common.utils;

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

/**
 * @author myh
 * @description: TODO
 * @date 2023/12/12 16:28
 */
public class ImageOptimizationUtil {

    // 阈值0-255
    public static int YZ = 150;

    /**
     * 图像二值化处理
     */
    public static BufferedImage binarization(BufferedImage bi) throws IOException {

        // 获取当前图片的高,宽,ARGB
        int h = bi.getHeight();
        int w = bi.getWidth();
        int arr[][] = new int[w][h];

        // 获取图片每一像素点的灰度值
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                // getRGB()返回默认的RGB颜色模型(十进制)
                arr[i][j] = getImageGray(bi.getRGB(i, j));// 该点的灰度值
            }
        }

        // 构造一个类型为预定义图像类型,BufferedImage
        BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);

        // 和预先设置的阈值大小进行比较,大的就显示为255即白色,小的就显示为0即黑色
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                if (getGray(arr, i, j, w, h) > YZ) {
                    int white = new Color(255, 255, 255).getRGB();
                    bufferedImage.setRGB(i, j, white);
                } else {
                    int black = new Color(0, 0, 0).getRGB();
                    bufferedImage.setRGB(i, j, black);
                }
            }

        }
        return bufferedImage;
    }

    /**
     * 图像的灰度处理
     * 利用浮点算法:Gray = R*0.3 + G*0.59 + B*0.11;
     *
     * @param rgb 该点的RGB值
     * @return 返回处理后的灰度值
     */
    private static int getImageGray(int rgb) {
        String argb = Integer.toHexString(rgb);// 将十进制的颜色值转为十六进制
        // argb分别代表透明,红,绿,蓝 分别占16进制2位
        int r = Integer.parseInt(argb.substring(2, 4), 16);// 后面参数为使用进制
        int g = Integer.parseInt(argb.substring(4, 6), 16);
        int b = Integer.parseInt(argb.substring(6, 8), 16);
        int gray = (int) (r*0.28 + g*0.95 + b*0.11);
        return gray;
    }

    /**
     * 自己加周围8个灰度值再除以9,算出其相对灰度值
     *
     * @param gray
     * @param x 要计算灰度的点的横坐标
     * @param y 要计算灰度的点的纵坐标
     * @param w 图像的宽度
     * @param h 图像的高度
     * @return
     */
    public static int getGray(int gray[][], int x, int y, int w, int h) {
        int rs = gray[x][y] + (x == 0 ? 255 : gray[x - 1][y]) + (x == 0 || y == 0 ? 255 : gray[x - 1][y - 1])
                + (x == 0 || y == h - 1 ? 255 : gray[x - 1][y + 1]) + (y == 0 ? 255 : gray[x][y - 1])
                + (y == h - 1 ? 255 : gray[x][y + 1]) + (x == w - 1 ? 255 : gray[x + 1][y])
                + (x == w - 1 || y == 0 ? 255 : gray[x + 1][y - 1])
                + (x == w - 1 || y == h - 1 ? 255 : gray[x + 1][y + 1]);
        return rs / 9;
    }

    /**
     * 二值化后的图像的开运算:先腐蚀再膨胀(用于去除图像的小黑点)
     * @throws IOException
     */
    public static BufferedImage opening(BufferedImage bi) throws IOException {

        // 获取当前图片的高,宽,ARGB
        int h = bi.getHeight();
        int w = bi.getWidth();
        int arr[][] = new int[w][h];
        // 获取图片每一像素点的灰度值
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                // getRGB()返回默认的RGB颜色模型(十进制)
                arr[i][j] = getImageGray(bi.getRGB(i, j));// 该点的灰度值
            }
        }

        int black = new Color(0, 0, 0).getRGB();
        int white = new Color(255, 255, 255).getRGB();
        BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
        // 临时存储腐蚀后的各个点的亮度
        int temp[][] = new int[w][h];
        // 1.先进行腐蚀操作
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                /*
                 * 为0表示改点和周围8个点都是黑,则该点腐蚀操作后为黑
                 * 由于公司图片态模糊,完全达到9个点全为黑的点太少,最后效果很差,故改为了小于30
                 * (写30的原因是,当只有一个点为白,即总共255,调用getGray方法后得到255/9 = 28)
                 */
                if (getGray(arr, i, j, w, h) < 30) {
                    temp[i][j] = 0;
                } else{
                    temp[i][j] = 255;
                }
            }
        }

        // 2.再进行膨胀操作
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                bufferedImage.setRGB(i, j, white);
            }
        }
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                // 为0表示改点和周围8个点都是黑,则该点腐蚀操作后为黑
                if (temp[i][j] == 0) {
                    bufferedImage.setRGB(i, j, black);
                    if(i > 0) {
                        bufferedImage.setRGB(i-1, j, black);
                    }
                    if (j > 0) {
                        bufferedImage.setRGB(i, j-1, black);
                    }
                    if (i > 0 && j > 0) {
                        bufferedImage.setRGB(i-1, j-1, black);
                    }
                    if (j < h-1) {
                        bufferedImage.setRGB(i, j+1, black);
                    }
                    if (i < w-1) {
                        bufferedImage.setRGB(i+1, j, black);
                    }
                    if (i < w-1 && j > 0) {
                        bufferedImage.setRGB(i+1, j-1, black);
                    }
                    if (i < w-1 && j < h-1) {
                        bufferedImage.setRGB(i+1, j+1, black);
                    }
                    if (i > 0 && j < h-1) {
                        bufferedImage.setRGB(i-1, j+1, black);
                    }
                }
            }
        }
        return bufferedImage;
    }

}


### 回答1: CentOS 7中的crontab是一种定时任务工具,可以用来在指定的时间自动执行某些命令或脚本。要使用crontab,您需要编辑crontab文件并添加要执行的命令或脚本。您可以使用以下命令来编辑crontab文件: crontab -e 然后,您可以按照以下格式添加定时任务: * * * * * command 其中,星号表示任何值,例如: - 第一个星号表示分钟(-59) - 第二个星号表示小时(-23) - 第三个星号表示日期(1-31) - 第四个星号表示月份(1-12) - 第五个星号表示星期几(-6,表示星期天) 例如,要在每天的早上6点运行一个脚本,您可以使用以下命令: 6 * * * /path/to/script.sh 这将在每天的6点运行名为“script.sh”的脚本。 ### 回答2: Crontab是一种在Linux系统上运行定时任务的标准方式。CentOS7作为一款流行的Linux发行版,自然也支持通过crontab工具来设置和管理定时任务。 在CentOS7上,使用crontab管理任务非常简单,只需要执行以下命令即可进入任务编辑器: ``` crontab -e ``` 通过执行上述命令,系统将打开一个vim编辑器,并且显示当前用户已经设置的所有计划任务。用户可以按照自己的需要,按照以下语法来设置和管理新的计划任务: ``` * * * * * command to be executed - - - - - | | | | | | | | | +----- day of the week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of the month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) ``` 上述语法中,星号(*)代表所有可能的值。例如,* * * * *表示每分钟都执行一次,0 0 * * *表示每天零点执行一次任务。用户还可以使用特定的值来指定特定的时间和日期,例如1-5表示一个范围内的数值,1,2,3表示多个数值,*/1表示每隔1个时间间隔执行一次任务。 在编辑器中,用户只需要按照上述语法设置任务。然后,根据需要设置执行命令和参数,以及输出和日志等相关选项。保存之后,任务即可自动在指定的时间和日期执行。 需要注意的是,为了确保定时任务能够正常工作,用户还需要在CentOS7中启用cron服务,方法如下: ``` systemctl enable crond.service service cron start ``` 执行上述命令后,cron服务即可设置成功,任务将会被执行。以上就是CentOS7 crontab定时任务的全部内容。 ### 回答3: Crontab是一个在Linux和Unix操作系统中用于管理定时任务的工具,CentOS7也支持使用Crontab进行定时任务的管理和配置。 一、使用Crontab进行定时任务的配置 在CentOS7中使用Crontab进行定时任务的配置需要遵循以下步骤: 1.使用命令`crontab -e`编辑Crontab配置文件。 2.在编辑窗口中添加需要执行的命令。 3.在Crontab配置文件中指定任务执行的时间和频率。 4.使用`:wq`命令保存并退出编辑窗口。 5.使用`crontab -l`命令查看已经设置的定时任务。 二、定时任务的语法和注意事项 在Crontab配置文件中指定任务执行的时间和频率需要遵循一定的语法,具体如下: ``` *  *  *  *  *  command 分 时 日 月 周 命令 ``` 其中,“分”表示一小时中的第几分钟(0~59),“时”表示一天中的第几小时(0~23),“日”表示一个月中的第几天(1~31),“月”表示一年中的第几个月(1~12),“周”表示一周中的第几天(0~6,其中0表示周日)。 在指定时间和频率时需要注意以下事项: 1.可以使用通配符*代替任意值。 2.可以使用逗号来分隔多个值。 3.可以使用连字符-来表示一个范围。 4.可以使用斜线/来指定时间间隔。 三、常见定时任务的配置示例 在CentOS7中,经常需要设置一些定时任务。一些常见的配置示例如下: 1.每天晚上0点30分备份MySQL数据库: ``` 30 0 * * * mysqldump -u root -p123456 abcdb > /home/backup/abcdb.sql ``` 2.每周六晚上10点进行系统更新: ``` 0 22 * * 6 yum update -y ``` 3.每小时检查一次服务器的磁盘空间使用率: ``` 0 * * * * df -h > /home/disk_usage.log ``` 总之,在CentOS7中使用Crontab管理定时任务是一件非常方便和实用的事情,只要按照指定的语法格式来配置定时任务,就可以方便地进行各种任务的定时运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值