A barcode is an optical machine-readable representation of data relating to the object to which it is attached.QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode).
Various Android applications came on Google Play for scanning QR code and barcodes. For integrating QR code in Android you can follow my blog How To Integrate Barcode/QrCode Scanner For Android?
In this post, I would like to show barcode/QrCode scanner for android using Zxing library without camera intent.We can scan QR code directly from a image which can be browsed directly from sd-card.
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//MainActivity.java
Download Sample Code
Have a look on few code snippets,
//MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package com.example.harshalbenake.decodeqrcode; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import com.google.zxing.BinaryBitmap; import com.google.zxing.MultiFormatReader; import com.google.zxing.RGBLuminanceSource; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer; import org.json.JSONObject; import org.json.XML; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { decodeQRCode(); } }); } public void decodeQRCode(){ try { File directory = new File (Environment.getExternalStorageDirectory() + "/hb"); File file = new File(directory, "hbcode.jpg"); //or any other format supported FileInputStream inputStream = new FileInputStream(file); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); if (bitmap == null) { System.out.println("uri is not a bitmap," + inputStream.toString()); } int width = bitmap.getWidth(), height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); bitmap.recycle(); bitmap = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source)); MultiFormatReader reader = new MultiFormatReader(); Result result = reader.decode(bBitmap); System.out.println("XML result: "+result); JSONObject xmlJSONObj = XML.toJSONObject(result.toString()); String jsonPrettyPrintString = xmlJSONObj.toString(); System.out.println(jsonPrettyPrintString); System.out.println("JSON result: "+jsonPrettyPrintString); } catch (Exception e) { e.printStackTrace(); } } } |
No comments:
Post a Comment