Screen shots of this example:
Note: This application done by using Android Studio Tool.
Step 1:
Open activity_main.xml file and Add a Scanner button. you can copy from below.
Step 2:
Open MainActivity.Java file Add Scanner button on click event to open Scanner Activity.
Step 3:
Crate barcode_scanner.xml layout file in layout folder. you can add Frame Layout for showing camera in that as below.
Step 4:
Open build.gradle file and add a zbar library is 'me.dm7.barcodescanner:zbar:1.8.2' in dependencies and sync that library.
Step 5:
Crate "CameraPreview.Java" file add the below logic in your file.
Step 6:
Create "BarcodeScanner.Java" file and add the below logic.
Step 7:
Open "Android Manifest.xml" file initiate BarcodeScanner Activity, and Add "CAMERA" permission in your manifest.
Step 8:
If you want Meterial Design for your app, create "color.xml" file in you values folder and add the below colors in it.
Step 9:
Open "styles.xml" file and add the below code.
Download Source Code
Note: This application done by using Android Studio Tool.
Open activity_main.xml file and Add a Scanner button. you can copy from below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/scannerButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="Scanner" /> </RelativeLayout>
Step 2:
Open MainActivity.Java file Add Scanner button on click event to open Scanner Activity.
package com.kvprasad.zbarbarcodescanner; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button scannerButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scannerButton = (Button) findViewById(R.id.scannerButton); scannerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(v.getContext(), BarcodeScanner.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Step 3:
Crate barcode_scanner.xml layout file in layout folder. you can add Frame Layout for showing camera in that as below.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cameraContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/cameraPreview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/ScanButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:text="Scan" /> </LinearLayout> </LinearLayout>
Step 4:
Open build.gradle file and add a zbar library is 'me.dm7.barcodescanner:zbar:1.8.2' in dependencies and sync that library.
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.kvprasad.zbarbarcodescanner" minSdkVersion 13 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'me.dm7.barcodescanner:zbar:1.8.2' }
Step 5:
Crate "CameraPreview.Java" file add the below logic in your file.
package com.kvprasad.zbarbarcodescanner; import android.content.Context; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; private Camera.PreviewCallback previewCallback; private Camera.AutoFocusCallback autoFocusCallback; public CameraPreview(Context context, Camera camera, Camera.PreviewCallback previewCb, Camera.AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use * software auto-focus. Only works for API level >=9. */ /* Camera.Parameters parameters = camera.getParameters(); for (String f : parameters.getSupportedFocusModes()) { if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autoFocusCallback = null; break; } } */ // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder holder) { try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { Log.d("DBG", "Error setting camera preview: " + e.getMessage()); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { /* * If your preview can change or rotate, take care of those events here. * Make sure to stop the preview before resizing or reformatting it. */ if (mHolder.getSurface() == null) { // preview surface does not exist return; } // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e) { // ignore: tried to stop a non-existent preview } try { // Hard code camera surface rotation 90 degs to match Activity view in portrait mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay(mHolder); mCamera.setPreviewCallback(previewCallback); mCamera.startPreview(); mCamera.autoFocus(autoFocusCallback); } catch (Exception e) { Log.d("DBG", "Error starting camera preview: " + e.getMessage()); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }
Step 6:
Create "BarcodeScanner.Java" file and add the below logic.
package com.kvprasad.zbarbarcodescanner; import android.content.DialogInterface; import android.content.pm.ActivityInfo; import android.hardware.Camera; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import net.sourceforge.zbar.Config; import net.sourceforge.zbar.Image; import net.sourceforge.zbar.ImageScanner; import net.sourceforge.zbar.Symbol; import net.sourceforge.zbar.SymbolSet; /** * Created by kvprasad on 10/3/2015. */ public class BarcodeScanner extends AppCompatActivity { private Camera mCamera; private CameraPreview mPreview; private Handler autoFocusHandler; private Button scanButton; private ImageScanner scanner; private boolean barcodeScanned = false; private boolean previewing = true; static { System.loadLibrary("iconv"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.barcode_scanner); initControls(); } private void initControls() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); autoFocusHandler = new Handler(); mCamera = getCameraInstance(); // Instance barcode scanner scanner = new ImageScanner(); scanner.setConfig(0, Config.X_DENSITY, 3); scanner.setConfig(0, Config.Y_DENSITY, 3); mPreview = new CameraPreview(BarcodeScanner.this, mCamera, previewCb, autoFocusCB); FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview); preview.addView(mPreview); scanButton = (Button) findViewById(R.id.ScanButton); scanButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (barcodeScanned) { barcodeScanned = false; mCamera.setPreviewCallback(previewCb); mCamera.startPreview(); previewing = true; mCamera.autoFocus(autoFocusCB); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { releaseCamera(); } return super.onKeyDown(keyCode, event); } /** * A safe way to get an instance of the Camera object. */ public static Camera getCameraInstance() { Camera c = null; try { c = Camera.open(); } catch (Exception e) { } return c; } private void releaseCamera() { if (mCamera != null) { previewing = false; mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } } private Runnable doAutoFocus = new Runnable() { public void run() { if (previewing) mCamera.autoFocus(autoFocusCB); } }; Camera.PreviewCallback previewCb = new Camera.PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); Camera.Size size = parameters.getPreviewSize(); Image barcode = new Image(size.width, size.height, "Y800"); barcode.setData(data); int result = scanner.scanImage(barcode); if (result != 0) { previewing = false; mCamera.setPreviewCallback(null); mCamera.stopPreview(); SymbolSet syms = scanner.getResults(); for (Symbol sym : syms) { Log.i("<<<<<<Asset Code>>>>> ", "<<<<Bar Code>>> " + sym.getData()); String scanResult = sym.getData().trim(); showAlertDialog(scanResult); /* Toast.makeText(BarcodeScanner.this, scanResult, Toast.LENGTH_SHORT).show();*/ barcodeScanned = true; break; } } } }; // Mimic continuous auto-focusing Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { autoFocusHandler.postDelayed(doAutoFocus, 1000); } }; private void showAlertDialog(String message) { new AlertDialog.Builder(this) .setTitle(getResources().getString(R.string.app_name)) .setCancelable(false) .setMessage(message) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .show(); } }
Step 7:
Open "Android Manifest.xml" file initiate BarcodeScanner Activity, and Add "CAMERA" permission in your manifest.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kvprasad.zbarbarcodescanner"> <uses-permission android:name="android.permission.CAMERA"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BarcodeScanner" android:label="@string/app_name" /> </application> </manifest>
Step 8:
If you want Meterial Design for your app, create "color.xml" file in you values folder and add the below colors in it.
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#125688</color> <color name="colorPrimaryDark">#125688</color> <color name="textColorPrimary">#FFFFFF</color> <color name="windowBackground">#FFFFFF</color> <color name="navigationBarColor">#000000</color> <color name="colorAccent">#c8e8ff</color> </resources>
Step 9:
Open "styles.xml" file and add the below code.
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <!-- <item name="windowNoTitle">true</item>--> <item name="windowActionBar">true</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>




