nosernur 发表于 2013-1-28 18:53:08

qrcode二维码学习笔记之二

原地址: http://kantaotao.blog.163.com/blog/static/9468786200972745646902/

qrcode二维码学习笔记之二

二维码 2009-08-27 16:56:46 阅读157 评论1 字号:大中小
<div class="bct fc05 fc11 nbw-blog ztag js-fs2">   
在Servlet中生成qrcode图片
//设置响应头信息
  String encode = "jpeg";
  response.setContentType("image/".concat(String.valueOf(String
    .valueOf(encode))));
  javax.servlet.ServletOutputStream outb = response.getOutputStream();
  response.setHeader("Pragma", "no-cache");
  response.setHeader("Cache-Control", "no-cache");
  response.setDateHeader("Expires", 0);
//从请求中读取内容
  InputStream input = request.getInputStream();
  DataInputStream dinput = new DataInputStream(input);
  String str = "";
  try {
   str = dinput.readUTF();
  } catch (Exception e) {
  }
  if ("".equals(str)) {
   str = "0";
  }
  System.out.println("str:" + str);
  //以下是测试数据
  EnQRCode barCode = QrCodeUtils.enqrcode(TestDBUtils.getDBString(str), 2);
  BufferedImage BarImage = new BufferedImage(barCode.getSize().width,
    barCode.getSize().height, 1);
  Graphics2D BarGraphics = BarImage.createGraphics();
  barCode.paint(BarGraphics);
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outb);
  encoder.encode(BarImage);
 
使用J2ME客户端生成图片信息
   HttpConnection  conn = (HttpConnection) Connector.open(“http://127.0.0.1/QrcodeDemo/do”,
     Connector.READ_WRITE, true);
   conn.setRequestMethod("POST");
   conn.setRequestProperty("Connection", "close");
   conn.setRequestProperty("Content-Type", "application/octet-stream");
   int sendlength =  str.getBytes().length;
   conn.setRequestProperty("Content-Length", String.valueOf(sendlength));
//打开流对象  并输入测试数据
   os = conn.openDataOutputStream();
   os.writeUTF("测试");
//获取服务器返回消息
   is = conn.openDataInputStream();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int ch = 0;
            while ((ch = is.read()) != -1)
            {
                baos.write(ch);
              
            }
            byte[] imageData = baos.toByteArray();
//生成图片信息
        Image image = Image.createImage(imageData, 0, imageData.length);
//在CANVAS中 画出qrcode图片
g.drawImage(image , width/2,fontheight * 3, Graphics.HCENTER| Graphics.TOP);
http://img.bimg.126.net/photo/outLqXf8ojfVBcdJLeRqvA==/3380514470295884406.jpg
http://img.bimg.126.net/photo/NpfuuNgSlen8Hklhvh5lEg==/3380514470295884404.jpg
页: [1]
查看完整版本: qrcode二维码学习笔记之二