/* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.JUtils.QRCode; import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.awt.image.BufferedImage; /** * Writes a {@link com.google.zxing.common.BitMatrix} to {@link java.awt.image.BufferedImage}, * file or stream. Provided here instead of core since it depends on * Java SE libraries. * * @author Sean Owen */ public final class MatrixToImageWriter { private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig(); private MatrixToImageWriter() {} /** * Renders a {@link com.google.zxing.common.BitMatrix} as an image, where "false" bits are rendered * as white, and "true" bits are rendered as black. */ public static BufferedImage toBufferedImage(BitMatrix matrix) { return toBufferedImage(matrix, DEFAULT_CONFIG); } /** * As {@link #toBufferedImage(com.google.zxing.common.BitMatrix)}, but allows customization of the output. */ public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel()); int onColor = config.getPixelOnColor(); int offColor = config.getPixelOffColor(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor); } } return image; } /** * Writes a {@link com.google.zxing.common.BitMatrix} to a file. * * @see #toBufferedImage(com.google.zxing.common.BitMatrix) */ public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { writeToFile(matrix, format, file, DEFAULT_CONFIG); } /** * As {@link #writeToFile(com.google.zxing.common.BitMatrix, String, java.io.File)}, but allows customization of the output. */ public static void writeToFile(BitMatrix matrix, String format, File file, MatrixToImageConfig config) throws IOException { BufferedImage image = toBufferedImage(matrix, config); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } /** * Writes a {@link com.google.zxing.common.BitMatrix} to a stream. * * @see #toBufferedImage(com.google.zxing.common.BitMatrix) */ public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { writeToStream(matrix, format, stream, DEFAULT_CONFIG); } /** * As {@link #writeToStream(com.google.zxing.common.BitMatrix, String, java.io.OutputStream)}, but allows customization of the output. */ public static void writeToStream(BitMatrix matrix, String format, OutputStream stream, MatrixToImageConfig config) throws IOException { BufferedImage image = toBufferedImage(matrix, config); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }

dapeng0011 LV15
2024年7月5日
3334004690 LV10
2024年6月6日
lee123321 LV22
2023年12月19日
初心不负丶方得始终 LV11
2023年4月21日
爱情戴罪的羔羊 LV7
2022年9月17日
guojun LV10
2022年8月17日
iizuidaima LV11
2022年8月11日
13940562934 LV22
2022年7月19日
姜广坤 LV14
2022年5月20日
crosa_Don LV18
2022年4月1日

ma406805131 LV19
2024年12月8日
dapeng0011 LV15
2024年7月5日
sky1044 LV1
2024年2月19日
lee123321 LV22
2023年12月19日
shiaomon
2023年12月15日
暂无贡献等级
3334004690 LV10
2023年11月1日
lilu0226 LV7
2023年10月29日
飘逸的云 LV1
2023年7月6日
初心不负丶方得始终 LV11
2023年4月21日
微信网友_6411404521459712
2023年3月30日
暂无贡献等级