-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efa4b55
commit d567152
Showing
10 changed files
with
249 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
QRGenearator/src/main/java/androidmads/library/qrgenearator/BarcodeEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
QRGenearator/src/main/java/androidmads/library/qrgenearator/BarcodeFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,4 @@ public static final class Type { | |
ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE | ||
}; | ||
} | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |