import com.cab.passport.advertising.dao.BannerDao; import com.cab.passport.petstore.dao.PetStoreDao; import com.cab.passport.petstore.entity.PetStore; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.thinkgem.jeesite.modules.sys.entity.User; import org.apache.commons.lang3.RandomStringUtils; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.util.*; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath*:/spring-context*.xml"}) public class T { @Autowired PetStoreDao petStoreDao; @Autowired private BannerDao bannerDao; // @Test public void a() { int count=bannerDao.getCount(); System.out.println(count); } // @Test public void b() throws Exception { /** * 生成二维码 */ List petStoreList = petStoreDao.findAllList(new PetStore()); System.out.println(petStoreList.size()); for (PetStore po : petStoreList) { if (Integer.parseInt(po.getId()) > 600) { System.out.println(po.getId() + "---+" + po.getInviteCode()); matrix(po.getId(), po.getInviteCode()); } } } private void matrix(String petStoreId, String inviteCode) throws Exception { //生成支付二维码 String text = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaf5a9e02cc14e220&redirect_uri=http%3A%2F%2Fpetsbox.chongaibao.com%2F%3FpetStoreId%3D" + petStoreId + "%23%2FcabPassList%2F0&response_type=code&scope=snsapi_base&state=#wechat_redirect"; int width = 142; int height = 142; Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage bitMatrixBufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); int[] imageArrayFirst = new int[width * height]; imageArrayFirst = bitMatrixBufferedImage.getRGB(0, 0, width, height, imageArrayFirst, 0, width); BufferedImage bufferedImage = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_RGB); bufferedImage.setRGB(0, 0, width, height, imageArrayFirst, 0, width); bufferedImage.setRGB(0, height, width, 20, inviteCode(inviteCode), 0, width); ImageIO.write(bufferedImage, "png", new FileOutputStream("D:/210张邀请码/" + petStoreId + ".png"));// 输出png图片 } private int[] inviteCode(String inviteCode) { BufferedImage bi = new BufferedImage(142, 20, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) bi.getGraphics(); g2.setColor(Color.WHITE);//设置颜色 g2.fillRect(0, 0, 142, 20);//填充整张图片,设置背景色RED g2.setColor(Color.BLACK);//设置颜色 g2.drawString("邀请码:" + inviteCode, 16, 15); return bi.getRGB(0, 0, 142, 20, null, 0, 142); } }