Saturday 16 January 2016

HB Blog 102: How To Decode QR Code From An Image Programmtically???

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
 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();
        }
    }
}

Saturday 2 January 2016

HB Blog 101: Vector Drawables - SVG To XML.

In Android development, and similar other mobile application development we observe that user interface and user experience plays the most vital roles. Specifically, in Android we have drawable making the application attractive. But, we also see that the drawable should be maintained for different densities, which eventually increases the size of the Apk as well as the application can become heavy.

Android came with the concept of vector drawable replacing old .png images with .xml vectors. These vector images are not only light weight but also gives more clear user interface for icons.
This lets you create a drawable based on an XML vector graphic. It can be defined in an XML file with the <vector> element.

Here is a simple VectorDrawable in this vectordrawable.xml file.

//vectordrawable.xml
 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
 <vector xmlns:android="http://schemas.android.com/apk/res/android"

     android:height="64dp"

     android:width="64dp"

     android:viewportHeight="600"

     android:viewportWidth="600" >

     <group

         android:name="rotationGroup"

         android:pivotX="300.0"

         android:pivotY="300.0"

         android:rotation="45.0" >

         <path

             android:name="v"

             android:fillColor="#000000"

             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />

     </group>

 </vector>