1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
| import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.*; import java.io.*; import java.net.URL; import java.util.*;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils; import org.slf4j.*;
import com.google.zxing.*; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeUtils {
private static final int LOGO_WIDTH = 110;
private static final int LOGO_HEIGHT = LOGO_WIDTH;
private static final int LOGO_FRAME_WIDTH = 5;
private static final int LOGO_FRAME_COLOR = 0xffffffff;
private static final int LOGO_BORDER_COLOR = 0x00000000;
private static final int LOGO_RADIUS = 10;
private static final int QRCODE_FOREGROUND_COLOR = 0x00000000;
private static final int QRCODE_BACKGROUND_COLOR = 0xffffffff;
private static final int QRCODE_SPECIFIES_MARGIN = 1;
private static final MultiFormatWriter mutiWriter = new MultiFormatWriter();
private static final Logger logger = LoggerFactory.getLogger(QRCodeUtils.class);
public static void generateToStream(String content, int width, int height, InputStream logo, OutputStream out) { try { ImageIO.write(genQRCode(content, width, height, logo), "jpg", out); } catch (IOException e) { e.printStackTrace(); logger.warn("Oops.", e); } catch (WriterException e) { e.printStackTrace(); logger.warn("Oops.", e); } }
private static BufferedImage genQRCode(String content, int width, int height, InputStream logo) throws WriterException, IOException { int r = LOGO_RADIUS;
int logoPixels[][] = null; boolean hasLogo = false; if (logo != null) { try { logoPixels = getLogoPixels(scale(logo, LOGO_WIDTH, LOGO_HEIGHT, true), r); hasLogo = true; } catch (IOException e) { e.printStackTrace(); logger.warn("Oops. Read the logo failed.", e); } }
Map<EncodeHintType, Object> hint = new HashMap<>(); hint.put(EncodeHintType.CHARACTER_SET, "utf-8"); hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.put(EncodeHintType.MARGIN, QRCODE_SPECIFIES_MARGIN);
BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hint);
int foregroundColor = QRCODE_FOREGROUND_COLOR; int backgroundColor = QRCODE_BACKGROUND_COLOR;
int halfW = matrix.getWidth() / 2; int halfH = matrix.getHeight() / 2; int[] pixels = new int[width * height]; int logoHalfWidth = LOGO_WIDTH / 2; int near = halfW - logoHalfWidth - LOGO_FRAME_WIDTH + r; int far = halfW + logoHalfWidth + LOGO_FRAME_WIDTH - r;
for (int y = 0; y < matrix.getHeight(); y++) { for (int x = 0; x < matrix.getWidth(); x++) { if (!hasLogo) { pixels[y * width + x] = matrix.get(x, y) ? foregroundColor : backgroundColor; } else { if (x > halfW - logoHalfWidth && x < halfW + logoHalfWidth && y > halfH - logoHalfWidth && y < halfH + logoHalfWidth) { pixels[y * width + x] = logoPixels[x - halfW + logoHalfWidth][y - halfH + logoHalfWidth]; } else if ((x > halfW - logoHalfWidth - LOGO_FRAME_WIDTH && x < halfW - logoHalfWidth + LOGO_FRAME_WIDTH && y > halfH - logoHalfWidth - LOGO_FRAME_WIDTH && y < halfH + logoHalfWidth + LOGO_FRAME_WIDTH) || (x > halfW + logoHalfWidth - LOGO_FRAME_WIDTH && x < halfW + logoHalfWidth + LOGO_FRAME_WIDTH && y > halfH - logoHalfWidth - LOGO_FRAME_WIDTH && y < halfH + logoHalfWidth + LOGO_FRAME_WIDTH) || (x > halfW - logoHalfWidth - LOGO_FRAME_WIDTH && x < halfW + logoHalfWidth + LOGO_FRAME_WIDTH && y > halfH - logoHalfWidth - LOGO_FRAME_WIDTH && y < halfH - logoHalfWidth + LOGO_FRAME_WIDTH) || (x > halfW - logoHalfWidth - LOGO_FRAME_WIDTH && x < halfW + logoHalfWidth + LOGO_FRAME_WIDTH && y > halfH + logoHalfWidth - LOGO_FRAME_WIDTH && y < halfH + logoHalfWidth + LOGO_FRAME_WIDTH)) {
if (x < near && y < near && (near - x) * (near - x) + (near - y) * (near - y) > r * r) { pixels[y * width + x] = matrix.get(x, y) ? foregroundColor : backgroundColor; } else if (x > far && y < near && (x - far) * (x - far) + (near - y) * (near - y) > r * r) { pixels[y * width + x] = matrix.get(x, y) ? foregroundColor : backgroundColor; } else if (x < near && y > far && (near - x) * (near - x) + (y - far) * (y - far) > r * r) { pixels[y * width + x] = matrix.get(x, y) ? foregroundColor : backgroundColor; } else if (x > far && y > far && (x - far) * (x - far) + (y - far) * (y - far) > r * r) { pixels[y * width + x] = matrix.get(x, y) ? foregroundColor : backgroundColor; } else { pixels[y * width + x] = backgroundColor; } } else { pixels[y * width + x] = matrix.get(x, y) ? foregroundColor : backgroundColor; } }
} }
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getRaster().setDataElements(0, 0, width, height, pixels);
return image; }
public static int[][] getLogoPixels(BufferedImage logo, int radius) {
int borderColor = LOGO_BORDER_COLOR; int whiteColor = LOGO_FRAME_COLOR;
int srcPixels[][] = new int[LOGO_WIDTH][LOGO_HEIGHT];
int r = radius; int max = LOGO_WIDTH;
for (int x = 0; x < LOGO_WIDTH; x++) { for (int y = 0; y < LOGO_HEIGHT; y++) { if (x < r && y < r && ((r - x) * (r - x) + (r - y) * (r - y) > (r - 1) * (r - 1))) { if ((r - x) * (r - x) + (r - y) * (r - y) > r * r) { srcPixels[x][y] = whiteColor; } else { srcPixels[x][y] = borderColor; } } else if (x > (max - r) && y < r && (x + r - max) * (x + r - max) + (r - y) * (r - y) > (r - 1) * (r - 1)) { if ((x + r - max) * (x + r - max) + (r - y) * (r - y) > r * r) { srcPixels[x][y] = whiteColor; } else { srcPixels[x][y] = borderColor; } } else if (x < r && y > (max - r) && (r - x) * (r - x) + (y + r - max) * (y + r - max) > (r - 1) * (r - 1)) { if ((r - x) * (r - x) + (y + r - max) * (y + r - max) > r * r) { srcPixels[x][y] = whiteColor; } else { srcPixels[x][y] = borderColor; } } else if (x > (max - r) && y > (max - r) && (x + r - max) * (x + r - max) + (y + r - max) * (y + r - max) > (r - 1) * (r - 1)) { if ((x + r - max) * (x + r - max) + (y + r - max) * (y + r - max) > r * r) { srcPixels[x][y] = whiteColor; } else { srcPixels[x][y] = borderColor; } } else { if (((x >= r && x <= max - r) && (y == 0 || y == 1 || y == max - 1 || y == max)) || ((y >= r && y <= max - r) && (x == 0 || x == 1 || x == max - 1 || x == max))) { srcPixels[x][y] = borderColor; } else { srcPixels[x][y] = logo.getRGB(x, y); } } } } return srcPixels; }
private static BufferedImage scale(InputStream src, int height, int width, boolean hasFiller) throws IOException { double ratio = 0.0D; BufferedImage srcImage = ImageIO.read(src); Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) { if (srcImage.getHeight() > srcImage.getWidth()) { ratio = (new Integer(height)).doubleValue() / srcImage.getHeight(); } else { ratio = (new Integer(width)).doubleValue() / srcImage.getWidth(); } AffineTransformOp op = new AffineTransformOp( AffineTransform.getScaleInstance(ratio, ratio), null); destImage = op.filter(srcImage, null); } if (hasFiller) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphic = image.createGraphics(); graphic.setColor(Color.white); graphic.fillRect(0, 0, width, height); if (width == destImage.getWidth(null)) graphic.drawImage(destImage, 0, (height - destImage.getHeight(null)) / 2, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); else graphic.drawImage(destImage, (width - destImage.getWidth(null)) / 2, 0, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); graphic.dispose(); destImage = image; } return (BufferedImage) destImage; }
public static void main(String[] args) { InputStream in = null; try { in = new URL("https://dn-codingon.qbox.me/img/avatar/me.jpeg").openStream(); generateToStream("https://www.haoyizebo.com", 500, 500, in, new FileOutputStream("/Users/yibo/Desktop/qrcode.jpg")); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(in); } } }
|