适用于Android的条形码/ Qr代码阅读器

前端之家收集整理的这篇文章主要介绍了适用于Android的条形码/ Qr代码阅读器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的应用程序中实现QR Code / Barcode阅读器.我想知道什么是最轻量级的解决方案(无视zxing的意图集成商).

解决方法

我用zxing构建了我的应用程序.你需要一些编码.首先包括core.jar,它在core / core.jar,在你的构建路径中,然后转到他们的客户端,在 android /…./ com.google.zxing,并获取他们的代码(这不是由开发者,因为你的副本和粘贴.)最后,添加代码
  1. package com.wtsang02.activities;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import com.google.zxing.BarcodeFormat;
  16. import com.google.zxing.BinaryBitmap;
  17. import com.google.zxing.ChecksumException;
  18. import com.google.zxing.FormatException;
  19. import com.google.zxing.LuminanceSource;
  20. import com.google.zxing.MultiFormatReader;
  21. import com.google.zxing.NotFoundException;
  22. import com.google.zxing.Reader;
  23. import com.google.zxing.Result;
  24. import com.google.zxing.ResultPoint;
  25. import com.google.zxing.common.HybridBinarizer;
  26.  
  27.  
  28. public class QRDecoder extends Activity implements OnClickListener {
  29.  
  30. private String text;
  31. private Button webbutton;
  32. private Bitmap bmp;
  33. private ImageView ivPicture;
  34. private TextView textv;
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.mysales);
  39. webbutton = (Button)findViewById(R.id.webbutton);
  40.  
  41. ivPicture = (ImageView) findViewById(R.id.ivPicture);
  42. textv= (TextView) findViewById(R.id.mytext);
  43.  
  44. webbutton.setOnClickListener(this);
  45. }
  46.  
  47. private void decode() {
  48.  
  49.  
  50. if (bmp == null) {
  51. Log.i("tag","wtf");
  52. }
  53. bmp = bmp.copy(Bitmap.Config.ARGB_8888,true);
  54.  
  55. int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
  56. bmp.getPixels(intArray,bmp.getWidth(),bmp.getHeight());
  57.  
  58. LuminanceSource source = new com.google.zxing.RGBLuminanceSource(
  59. bmp.getWidth(),bmp.getHeight(),intArray);
  60. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  61. Reader reader = new MultiFormatReader();
  62. try {
  63. Result result = reader.decode(bitmap);
  64.  
  65. text = result.getText();
  66. byte[] rawBytes = result.getRawBytes();
  67. BarcodeFormat format = result.getBarcodeFormat();
  68. ResultPoint[] points = result.getResultPoints();
  69. textv.setText(text);
  70.  
  71. } catch (NotFoundException e) {
  72.  
  73. e.printStackTrace();
  74. } catch (ChecksumException e) {
  75.  
  76. e.printStackTrace();
  77. } catch (FormatException e) {
  78.  
  79. e.printStackTrace();
  80.  
  81. }
  82. Log.i("done","done");
  83. if(text!=null)
  84. Toast.makeText(getBaseContext(),text,Toast.LENGTH_LONG).show();
  85. else{
  86. Toast.makeText(getBaseContext(),"QQ",Toast.LENGTH_LONG).show();
  87. }
  88. }
  89.  
  90. @Override
  91. public void onClick(View v) {
  92.  
  93. Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  94. startActivityForResult(i,0);
  95. }
  96.  
  97. @Override
  98. protected void onActivityResult(int requestCode,int resultCode,Intent data) {
  99. super.onActivityResult(requestCode,resultCode,data);
  100. if (resultCode == RESULT_OK) {
  101. Bundle extras = data.getExtras();
  102. bmp = (Bitmap) extras.get("data");
  103. ivPicture.setImageBitmap(bmp);
  104. decode();
  105. }
  106.  
  107. }
  108.  
  109. }

代码将使用您手机的默认摄像头,如果您需要使用他们的客户端,您将需要启动他们的CaptureActivity,您的布局应包括TextView以显示结果,ImageView显示您捕获的图像,以及按钮以启动摄像头. .这是基于2.1zxing.

猜你在找的Android相关文章