android直接从图片中对二维码编码解码

转载 2012年03月29日 11:30:09
001 package com.google.zxing.client.android;
002   
003 import java.util.Hashtable;
004   
005 import android.app.Activity;
006 import android.graphics.Bitmap;
007 import android.graphics.drawable.BitmapDrawable;
008 import android.os.Bundle;
009 import android.util.Log;
010 import android.view.View;
011 import android.view.View.OnClickListener;
012 import android.widget.Button;
013 import android.widget.EditText;
014 import android.widget.ImageView;
015 import android.widget.TextView;
016   
017 import com.google.zxing.BarcodeFormat;
018 import com.google.zxing.BinaryBitmap;
019 import com.google.zxing.ChecksumException;
020 import com.google.zxing.EncodeHintType;
021 import com.google.zxing.FormatException;
022 import com.google.zxing.NotFoundException;
023 import com.google.zxing.Result;
024 import com.google.zxing.WriterException;
025 import com.google.zxing.common.BitMatrix;
026 import com.google.zxing.common.HybridBinarizer;
027 import com.google.zxing.qrcode.QRCodeReader;
028 import com.google.zxing.qrcode.QRCodeWriter;
029 import com.test.RGBLuminanceSource;
030   
031 public class TestActivity extends Activity {
032     private static final String TAG = "TestActivity";
033       
034     String path;
035     private final static String pFormat = "png";
036   
037           
038   
039     protected void onCreate(Bundle savedInstanceState) {
040         super.onCreate(savedInstanceState);
041         setContentView(R.layout.testactivity);
042         //set
043         Button btn2 = (Button)findViewById(R.id.Button03);
044         btn2.setOnClickListener(new OnClickListener() {
045               
046             @Override
047             public void onClick(View view) {
048                 ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
049                 QRCodeReader reader = new QRCodeReader(); 
050                 int width = 200, height = 200;  
051                 QRCodeWriter writer = new QRCodeWriter();
052                 try {
053                     EditText edit = (EditText)findViewById(R.id.EditText01);
054                     //edit.getText();
055                     Log.i(TAG, "编辑框中的内容: " + edit.getText().toString());
056 //                  System.out.println(edit.getText().toString());
057                     if(edit.getText().toString().length()<1)
058                     {
059                         return;
060                     }
061                       
062                     BitMatrix martix = writer.encode(edit.getText().toString(), BarcodeFormat.QR_CODE, width, height);
063                     System.out.println("w:"+martix.width+"h:"+martix.height); 
064                     String imageFormat = "png"
065                     Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
066                     hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
067                 BitMatrix bitMatrix = new QRCodeWriter().encode(edit.getText().toString(),BarcodeFormat.QR_CODE, width, height,hints);
068                      int[] pixels = new int[width * height];    
069                      for (int y = 0; y < height; y++) {    
070                          for (int x = 0; x < width; x++) {    
071                              if(bitMatrix.get(x, y)){    
072                                  pixels[y * width + x] = 0xff000000;    
073                              }else{
074                                  pixels[y * width + x] = 0xffffffff;
075                              }    
076                                    
077                          }    
078                      }    
079                            
080                      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);    
081                           
082                      bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
083                      imgView.setImageBitmap(bitmap);
084 //                   RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
085 //                  LuminanceSource source = new RGBLuminanceSource(path);  
086 //                  BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));  
087 //                   QRCodeReader reader2= new QRCodeReader();
088 //                   Result result = reader2.decode(bitmap1);
089 //                   System.out.println("res"+result.getText());
090                        
091                 } catch (WriterException e) {
092                     // TODO Auto-generated catch block
093                     e.printStackTrace();
094                 }
095             }
096         });
097           
098         Button btn1 = (Button)findViewById(R.id.Button01);
099         btn1.setOnClickListener(new OnClickListener() {
100               
101             @Override
102             public void onClick(View v) {
103                 // TODO Auto-generated method stub
104                 ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
105                 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
106                 hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
107                 Bitmap bitmap = ((BitmapDrawable)imgView.getDrawable()).getBitmap();
108                 RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
109 //                  LuminanceSource source = new RGBLuminanceSource(path);  
110                     BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));  
111                      QRCodeReader reader2= new QRCodeReader();
112                      Result result;
113                    try {
114                      result = reader2.decode(bitmap1,hints);
115                      System.out.println("res"+result.getText());
116                      TextView text  = (TextView)findViewById(R.id.TextView01);
117                     text.setText(result.getText());
118                     } catch (NotFoundException e) {
119                         // TODO Auto-generated catch block
120                         e.printStackTrace();
121                     } catch (ChecksumException e) {
122                         // TODO Auto-generated catch block
123                         e.printStackTrace();
124                     } catch (FormatException e) {
125                         // TODO Auto-generated catch block
126                         e.printStackTrace();
127                     }
128                     // System.out.println("res"+result.getText());
129             }
130         });
131     }
132 }

相关文章推荐

二维码编码解码

  • 2016-06-08 11:55
  • 355KB
  • 下载

条形码和二维码编码解码

/** * 条形码和二维码编码解码 * * @author stronginfo * @version 2014-02-28 */ public class ZxingHandler { ...

二维码编码解码软件

  • 2016-05-06 17:01
  • 775KB
  • 下载

java 二维码编码解码

  • 2013-07-25 09:50
  • 2.00MB
  • 下载

二维码的编码与解码辅助类

需要的jar包:QRCode.jar 效果图: 以下为我封装的辅助类,提供的API只有: (1)encode(String s...

二维码编码解码源代码

  • 2011-01-13 15:58
  • 6.38MB
  • 下载

二维码的编码解码源代码

  • 2014-05-24 18:50
  • 1005KB
  • 下载

Java实现二维码QRCode的编码和解码

周末试用下Android手机的二维码扫描软件,扫描了下火车票、名片等等,觉得非常不错很有意思的。当然Java也可以实现这些,现在就分享下如何简单用Java实现二维码中QRCode的编码和解码(可以手机...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)