生成二维码并且将生成图片转换成输入流

生成二维码导入Ireport

图片直接转换为输入流


#

标题

在这里插入图片描述

一、生成二维码并且将图片转换成为输入流

代码如下(示例):

public BufferedImage addLogo(BufferedImage bufferedImage) {
        Graphics2D graphics2D = bufferedImage.createGraphics();
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();

        try {
        	//添加二维码 中心的Logo  也可以手动设定Logo位置
            BufferedImage logo = ImageIO.read(new File("D:\\阿葵.jpg"));
            graphics2D.drawImage(logo, width / 5 * 2, height / 5 * 2, width / 5, height / 5, null);
            BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL);
            graphics2D.setStroke(stroke);// 设置笔画对象
            //指定弧度的圆角矩形
            RoundRectangle2D.Float round = new RoundRectangle2D.Float(width / 5 * 2, height / 5 * 2, width / 5, height / 5, 20, 20);
            graphics2D.setColor(Color.white);
            graphics2D.draw(round);// 绘制圆弧矩形

            //设置logo 有一道灰色边框
            BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
            graphics2D.setStroke(stroke2);// 设置笔画对象
            RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width / 5 * 2 + 2, height / 5 * 2 + 2, width / 5 - 4, height / 5 - 4, 20, 20);
            graphics2D.setColor(new Color(128, 128, 128));
            graphics2D.draw(round2);// 绘制圆弧矩形

            graphics2D.dispose();
            bufferedImage.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bufferedImage;
    }



public InputStream createQrCode(int width, int height) {
        boolean flag = false;
        InputStream inputStream = null;

        if (width >= 0 && width <= 500 && height >= 0 && height <= 500) {
            Qrcode qrcode = new Qrcode();
            qrcode.setQrcodeVersion(4);//设置qrcode版本可选1~40
            qrcode.setQrcodeEncodeMode('B');//设置编码类型B表示二进制编码
            qrcode.setQrcodeErrorCorrect('M');//设置纠错类型M,表示15
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);//设置bufferImage宽高,色域
            Graphics2D graphics2D = bufferedImage.createGraphics();
            graphics2D.setBackground(Color.white);
            graphics2D.setColor(Color.BLACK);
            graphics2D.clearRect(0, 0, width, height);

            int prefix = 20;//设置偏移量
            try {
                byte[] bytes = content.getBytes("utf-8");
                if (bytes.length > 0 && bytes.length < 1000) {
                    boolean[][] booleans = qrcode.calQrcode(bytes);
                    for (int i = 0; i < booleans.length; i++) {
                        for (int j = 0; j < booleans.length; j++) {
                            if (booleans[i][j]) {
                                //写入图片
                                graphics2D.fillRect(i * 10 + prefix, j * 10 + prefix, 10, 10);
                            }
                        }
                    }
                }

                graphics2D.dispose();//关闭画图
                bufferedImage.flush();//刷新
                //addLogo(bufferedImage);
                //ImageIO.write(bufferedImage, "png", img);
                //ByteArrayOutputStream 是可以讲文件接收到成为一个outputStream
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                //ImageIo.write可以将文件转换成为一个输出流
                ImageIO.write(bufferedImage, "png",os);
                //ByteArrayInputStream  可以通过ByteArrayOutputStream os.toByteArray()输出的比特数组生成一个输出流  输出流->输入流
                inputStream=new ByteArrayInputStream(os.toByteArray());
                return inputStream;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream;
    }

2.读入数据

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

该处使用的url网络请求的数据。


总结

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值