Skip to content

Commit

Permalink
Issue (Enhancement) #24 resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
androidmads committed Sep 8, 2024
1 parent efa4b55 commit d567152
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 21 deletions.
1 change: 1 addition & 0 deletions QRGenearator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ android {

dependencies {
implementation 'com.google.zxing:core:3.5.0'
implementation 'com.google.zxing:android-integration:3.3.0'
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package androidmads.library.qrgenearator;

import android.graphics.Bitmap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import java.util.EnumMap;
import java.util.Map;

public class BarcodeEncoder {

private int WHITE = 0xFFFFFFFF;
private int BLACK = 0xFF000000;
private int dimension = 500; // Default size
private String contents = null;
private androidmads.library.qrgenearator.BarcodeFormat format = androidmads.library.qrgenearator.BarcodeFormat.CODE_128; // Default to CODE_128

public void setColorWhite(int color) {
this.WHITE = color;
}

public void setColorBlack(int color) {
this.BLACK = color;
}

public int getColorWhite() {
return this.WHITE;
}

public int getColorBlack() {
return this.BLACK;
}

public BarcodeEncoder(String data, androidmads.library.qrgenearator.BarcodeFormat format) {
this.contents = data;
this.format = format;
}

public BarcodeEncoder(String data, androidmads.library.qrgenearator.BarcodeFormat format, int dimension) {
this.contents = data;
this.format = format;
this.dimension = dimension;
}

public Bitmap getBitmap() {
return getBitmap(0);
}

public Bitmap getBitmap(int margin) {
if (contents == null || contents.isEmpty()) return null;
try {
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.MARGIN, margin);
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, convertToZXingFormat(format), dimension, dimension, hints);
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];

for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? getColorBlack() : getColorWhite();
}
}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}

public static BarcodeFormat convertToZXingFormat(androidmads.library.qrgenearator.BarcodeFormat aFormat) {
if (aFormat == null) {
throw new IllegalArgumentException("Format cannot be null");
}

switch (aFormat) {
case AZTEC:
return BarcodeFormat.AZTEC;
case CODABAR:
return BarcodeFormat.CODABAR;
case CODE_39:
return BarcodeFormat.CODE_39;
case CODE_93:
return BarcodeFormat.CODE_93;
case CODE_128:
return BarcodeFormat.CODE_128;
case DATA_MATRIX:
return BarcodeFormat.DATA_MATRIX;
case EAN_8:
return BarcodeFormat.EAN_8;
case EAN_13:
return BarcodeFormat.EAN_13;
case ITF:
return BarcodeFormat.ITF;
case MAXICODE:
return BarcodeFormat.MAXICODE;
case PDF_417:
return BarcodeFormat.PDF_417;
case QR_CODE:
return BarcodeFormat.QR_CODE;
case RSS_14:
return BarcodeFormat.RSS_14;
case RSS_EXPANDED:
return BarcodeFormat.RSS_EXPANDED;
case UPC_A:
return BarcodeFormat.UPC_A;
case UPC_E:
return BarcodeFormat.UPC_E;
case UPC_EAN_EXTENSION:
return BarcodeFormat.UPC_EAN_EXTENSION;
default:
throw new IllegalArgumentException("Unsupported format: " + aFormat);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package androidmads.library.qrgenearator;

public enum BarcodeFormat {

/** Aztec 2D barcode format. */
AZTEC,

/** CODABAR 1D format. */
CODABAR,

/** Code 39 1D format. */
CODE_39,

/** Code 93 1D format. */
CODE_93,

/** Code 128 1D format. */
CODE_128,

/** Data Matrix 2D barcode format. */
DATA_MATRIX,

/** EAN-8 1D format. */
EAN_8,

/** EAN-13 1D format. */
EAN_13,

/** ITF (Interleaved Two of Five) 1D format. */
ITF,

/** MaxiCode 2D barcode format. */
MAXICODE,

/** PDF417 format. */
PDF_417,

/** QR Code 2D barcode format. */
QR_CODE,

/** RSS 14 */
RSS_14,

/** RSS EXPANDED */
RSS_EXPANDED,

/** UPC-A 1D format. */
UPC_A,

/** UPC-E 1D format. */
UPC_E,

/** UPC/EAN extension format. Not a stand-alone format. */
UPC_EAN_EXTENSION

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ public static final class Type {
ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE
};
}

Binary file removed QRGenerator.zip
Binary file not shown.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ try {
Log.v(TAG, e.toString());
}
```
The following lines are used to generated the Bar Code
```java
BarcodeEncoder barcodeEncoder = new BarcodeEncoder(inputValue, BarcodeFormat.CODE_128, 800);
bitmap = barcodeEncoder.getBitmap(2); // Margin of 2 pixels
// Now you can use this bitmap as needed, e.g., display it in an ImageView
qrImage.setImageBitmap(bitmap);
```

Save QR Code as Image
```java
Expand Down
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
implementation project(':QRGenearator')
// implementation 'com.github.androidmads:QRGenerator:1.0.1'

// implementation project(':QRGenearator')
implementation 'com.github.androidmads:QRGenerator:1.0.2'
}
23 changes: 23 additions & 0 deletions app/src/main/java/androidmads/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.util.Objects;

import androidmads.library.qrgenearator.BarcodeEncoder;
import androidmads.library.qrgenearator.BarcodeFormat;
import androidmads.library.qrgenearator.QRGContents;
import androidmads.library.qrgenearator.QRGEncoder;
import androidmads.library.qrgenearator.QRGSaver;
Expand Down Expand Up @@ -57,6 +59,27 @@ protected void onCreate(Bundle savedInstanceState) {
}

findViewById(R.id.generate_barcode).setOnClickListener(view -> {
inputValue = edtValue.getText().toString().trim();
if (inputValue.length() > 0) {
try {

BarcodeEncoder barcodeEncoder = new BarcodeEncoder(inputValue,
BarcodeFormat.CODE_128, 800);
bitmap = barcodeEncoder.getBitmap(2); // Margin of 2 pixels

// Now you can use this bitmap as needed, e.g., display it in an ImageView

qrImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
} else {
edtValue.setError(getResources().getString(R.string.value_required));
}
});


findViewById(R.id.generate_qrcode).setOnClickListener(view -> {
inputValue = edtValue.getText().toString().trim();
if (inputValue.length() > 0) {
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
Expand Down
51 changes: 34 additions & 17 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
android:id="@+id/edt_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/enter_text"
android:importantForAutofill="no"
android:inputType="text"
android:layout_margin="8dp"
android:minHeight="48dp" />

<EditText
android:hint="Set Black Color"
android:text="#000000"
android:id="@+id/preview_selected_firstcolor"
android:minHeight="48dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Set Black Color"
android:importantForAutofill="no"
android:inputType="text"
android:minHeight="48dp"
android:text="#000000"
tools:ignore="HardcodedText" />


<EditText
android:hint="Set White Color"
android:text="#ffffff"
android:layout_height="wrap_content"
android:id="@+id/preview_selected_secondcolor"
android:minHeight="48dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:hint="Set White Color"
android:importantForAutofill="no"
android:inputType="text"
android:minHeight="48dp"
android:text="#ffffff"
tools:ignore="HardcodedText" />

<LinearLayout
Expand All @@ -52,22 +52,39 @@
android:orientation="horizontal">

<Button
android:importantForAutofill="no"
android:inputType="text"
android:id="@+id/generate_barcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/generate"
android:importantForAutofill="no"
android:inputType="text"
android:text="@string/generate_bar"
tools:ignore="ButtonStyle,TextViewEdits" />

<Button
android:id="@+id/generate_qrcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:importantForAutofill="no"
android:inputType="text"
android:text="@string/generate_qr"
tools:ignore="ButtonStyle,TextViewEdits" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">

<Button
android:id="@+id/save_barcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:importantForAutofill="no"
android:inputType="text"
android:text="@string/save"
tools:ignore="ButtonStyle,TextViewEdits" />

Expand All @@ -82,19 +99,19 @@
</LinearLayout>

<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="20sp"
android:text="Preview"
android:textSize="20sp"
tools:ignore="HardcodedText,UsingOnClickInXml" />

<ImageView
android:layout_marginTop="10dp"
android:id="@+id/qr_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:contentDescription="@string/app_name" />

</LinearLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<resources>
<string name="app_name">QRGenerator</string>
<string name="save">Save</string>
<string name="generate">Generate</string>
<string name="generate_bar">Generate Barcode</string>
<string name="generate_qr">Generate QR Code</string>
<string name="enter_text">Enter Text</string>
<string name="value_required">Value Required</string>
</resources>

0 comments on commit d567152

Please sign in to comment.