java 编写简单二维码demo(亲测可用)
时间:2021-11-10 作者:匿名
简单生成二维码图片
反正可以用就 直接拿过来用了。。
忘了从哪里来了 好久前下载的 。。
package 域名12.二维码; import 域名; import 域名table; import 域名域名odeFormat; import 域名域名deHintType; import 域名域名iFormatWriter; import 域名域名atrix; public class testQR { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String text = "http://域名"; int width = 300; int height = 300; //二维码的图片格式 String format = "gif"; Hashtable hints = new Hashtable(); //内容所使用编码 域名(域名ACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, 域名ODE, width, height, hints); //生成二维码 File outputFile = new File("d:"+域名rator+"域名"); 域名eToFile(bitMatrix, format, outputFile); } }
package 域名12.二维码; import 域名域名atrix; import 域名eIO; import 域名; import 域名utStream; import 域名ception; import 域名域名eredImage; public class QRUtil { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private QRUtil() {} public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = 域名idth(); int height = 域名eight(); BufferedImage image = new BufferedImage(width, height, 域名_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { 域名GB(x, y, 域名(x, y) ? BLACK : WHITE); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!域名e(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!域名e(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }
jar 包 下载地址 http://域名/lib/域名