In this video it shows the steps to program your Android App to generate/ create Bar Code for any text entered in your Android App. It uses the zxing library to generate the bar code format image for the entered Text. To start, this library should be included in the dependencies of the App’s build.gradle file so that the APIs of this library is available in the program/ App. It implements the below version of zxing library in this tutorial:
implementation ‘com.google.zxing:core:3.2.1’
In this App it creates a very simple layout in which a user can enter any text and then on clicking a button the onclick method generates the barcode using the zxing library APIs. The barcode is displayed using a imageview widget. Actual phone’s App is used to test and verify the generated barcode shown in this tutorial.
For any questions, suggestions, comments or appreciation we will be glad to hear from you at: programmerworld1990@gmail.com.
Test Results – Screenshots from the actual phone
Source code:
package com.example.mybarcodegenerator; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.Color; import android.media.Image; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; public class MainActivity extends AppCompatActivity { private EditText editText; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); imageView = findViewById(R.id.imageView); } public void barCodeButton(View view){ MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); try { BitMatrix bitMatrix = multiFormatWriter.encode(editText.getText().toString(), BarcodeFormat.CODE_128, imageView.getWidth(), imageView.getHeight()); Bitmap bitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.RGB_565); for (int i = 0; i<imageView.getWidth(); i++){ for (int j = 0; j<imageView.getHeight(); j++){ bitmap.setPixel(i,j,bitMatrix.get(i,j)? Color.BLACK:Color.WHITE); } } imageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="116dp" android:layout_marginTop="16dp" android:onClick="barCodeButton" android:text="@string/bar_code_generator" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText" /> <ImageView android:id="@+id/imageView" android:layout_width="242dp" android:layout_height="71dp" android:layout_marginStart="84dp" android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" app:srcCompat="?attr/actionBarDivider" android:contentDescription="@string/todo" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="99dp" android:layout_marginTop="12dp" android:ems="10" android:hint="@string/enter_the_text_here" android:inputType="textPersonName" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:autofillHints="" /> </androidx.constraintlayout.widget.ConstraintLayout>
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.example.mybarcodegenerator" minSdkVersion 26 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'com.google.zxing:core:3.2.1' }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mybarcodegenerator"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<resources> <string name="app_name">My Bar Code Generator</string> <string name="enter_the_text_here">Enter the Text Here ...</string> <string name="bar_code_generator">Bar Code Generator</string> <string name="todo">TODO</string> </resources>
hi I need to create in Adapter but i need to initialize only once in bind method. How I can do this?
Could you please elaborate a bit on your use case? It will help us in providing more precise response.
Here, in this App, it is using 3rd party implementation (zxing libraries) to generate the bar code for the text entered. It uses MultiFormatWriter object from these libraries for the same.
Cheers
Programmer World
–