使用ImageIO读取网络图片(二维码)或本地图片

使用ImageIO读取网络图片(二维码)或本地图片

一读取网络图片

    public static String analysisReadQRCode(String path){
     
        MultiFormatReader multiFormatReader=new MultiFormatReader();
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"GBK");
        try{
     
            URL url = new URL(path);
            BufferedImage source= ImageIO.read(url);
            BinaryBitmap binaryImg=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(source)));
            Result result=multiFormatReader.decode(binaryImg,hints);
            return result.getText();
        }catch (Exception e){
     
            e.printStackTrace();
        }
        return "";
    }

二读取网络图片

public static String analysisReadQRCode(String path){
     
        MultiFormatReader multiFormatReader=new MultiFormatReader();
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"GBK");
        try{
     
            BufferedImage source= ImageIO.read(new File(path));
            BinaryBitmap binaryImg=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(source)));
            Result result=multiFormatReader.decode(binaryImg,hints);
            return result.getText();
        }catch (Exception e){
     
            e.printStackTrace();
        }
        return "";
    }

你可能感兴趣的:(java)