android直接从图片中对二维码编码解码
16lz
2022-01-22
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 | } |
更多相关文章
- Android(安卓)采用Pull解析XML内容 【学习记录】
- 2015年11月广师android群内容分享
- android RecyclerView 设置设置选中的一行的内容
- 用android LinearLayout和RelativeLayout实现精确布局
- Android(安卓)7 强势来袭开发者们不得不知道的细节
- Android: 打印Bundle内容
- 转到编辑联系人的系统界面 编辑 添加
- Android(安卓)TabHost使用、动态加载内容
- Android: 打印Bundle内容