写点东西:《java处理图片相关》

9 篇文章 0 订阅
4 篇文章 0 订阅

故不积跬步,无以至千里;不积小流,无以成江海。 ——荀子《劝学篇》

每一篇优秀的文章都非一日写成,定期写点东西,有所积累,并不断完善。
今天借助网上各位前辈的优秀文章,尝试自己封装了一个图片的工具类,分享给大家。

话不多说,代码如下↓

package com.lm.util;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
 *
 * @package com.lm.util
 * @file QRcodeUtil.java 创建时间:2019年11月29日下午8:53:33
 * @title 二维码工具类
 * @description 
 * @author   mr.bone
 * @version 1.0
 *
 */
public class ImageUtil {
    /**
     * zxing 方式生成二维码操作对象(BitMatrix)
     */
    private static BitMatrix  createQRcode(String content,Map<String,Object> param) throws WriterException{
        final int width = (int) param.get("width");
        final int height = (int) param.get("height");
        final Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        return new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
    }
    /**
         * 生成二维码并放入输出流中
     */
    public static void createQRcodeToOutputStream(String content,Map<String,Object> param,OutputStream out) throws WriterException, IOException {
        final BitMatrix bitMatrix = createQRcode(content,param);
        //将二维码操作对象(BitMatrix)放入内存中图片缓存区并生成BufferedImage
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));
        final String format = (String) param.get("format");
        //将BufferedImage=>byte[]=>OutputStream
        ImageIO.write(bufferedImage,format,out);
    }
}

欲探其祥,api如下↓

接口:com.google.zxing.Writer
方法名返回值类型
encode(String contents, BarcodeFormat format, int width, int height)BitMatrix
encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints)BitMatrix
参数类型参数名描述
Stringcontents编码的内容(想要二维码不同,编码内容必须不同可通过随机数实现例如(String content = "http://aaaa.bbb.ccc?t="+Math.Random();
BarcodeFormatformat编码的方式(BarcodeFormat.QR_CODE:二维码、条形码…)
intwidth首选的宽度
intheight首选的高度
Map<EncodeHintType,?>hints编码时的额外参数

额外参数

参数说明
ERROR_CORRECTION容错率,指定容错等级,例如二维码中使用的ErrorCorrectionLevel, Aztec使用Integer(分为四个等级:L/M/Q/H, 等级越高,容错率越高,识别速度降低。例如一个角被损坏,容错率高的也许能够识别出来。通常为H)
CHARACTER_SET编码集(编码集,通常有中文,设置为 utf-8)
DATA_MATRIX_SHAPE指定生成的数据矩阵的形状,类型为SymbolShapeHint
MARGIN生成条码的时候使用,指定边距,单位像素,受格式的影响。类型Integer, 或String代表的数字类型
PDF417_COMPACT指定是否使用PDF417紧凑模式(具体含义不懂)类型Boolean
PDF417_COMPACTION指定PDF417的紧凑类型
PDF417_DIMENSIONS指定PDF417的最大最小行列数
AZTEC_LAYERSaztec编码相关,不理解
QR_VERSION指定二维码版本,版本越高越复杂,反而不容易解析
方法:ImageIO.write(BufferedImage image,String format,OutputStream out);
参数类型参数名描述
BufferedImageimageBufferedImage 对象
BarcodeFormatformat图片格式,gif,png等
OutputStreamout输出流

参考链接:
https://www.jianshu.com/p/6607e69b1121
https://blog.csdn.net/jiachunchun/article/details/89670721

随时补充,下期再见~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MR_Bone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值