BarCode, QRCode&Scan

2018. 11. 6. 13:22Android

반응형

o build.gradle(app)

implementation 'com.google.zxing:core:3.3.2'

   implementation 'com.journeyapps:zxing-android-embedded:3.5.0'


o BarCode.java 

public class BarCode {

private static final int WHITE = 0xFFFFFFFF; // 배경 색상
private static final int BLACK = 0xFF000000; // 바코드 색상

public static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height)
throws WriterException {

String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}

Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}

MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}

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) ? BLACK : WHITE;
}
}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

return bitmap;
}

private static String guessAppropriateEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}

}

o QRCode.java

public class QrCode {

public static Bitmap generateQRCode(String contents) {
Bitmap bitmap = null;

try {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
bitmap = toBitmap(qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, 600, 600));
} catch (WriterException e) {
e.printStackTrace();
}

return bitmap;
}

private static Bitmap toBitmap(BitMatrix matrix) {
int height = matrix.getHeight();
int width = matrix.getWidth();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
}

}

o  MainActivity.java

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();

private ImageView barcode;
private ImageView qrcode;
private Button scan;
private TextView mResult;

private IntentIntegrator integrator;


@TargetApi(Build.VERSION_CODES.P)
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

barcode = findViewById(R.id.barcode);
qrcode = findViewById(R.id.qrcode);
mResult = findViewById(R.id.result);

scan = findViewById(R.id.scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
integrator = new IntentIntegrator(MainActivity.this);
// integrator.setPrompt("scanning..");
integrator.setCaptureActivity(CodeCaptureActivity.class);
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
});

genBarCode();
genQrCode();

}


private void genBarCode(){
String barcode_data = "1234567890";

// barcode image
try {
Bitmap bitmap = BarCode.encodeAsBitmap(barcode_data , BarcodeFormat.CODE_128 , 800 , 300);
barcode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}

private void genQrCode(){
String data = "1234567890";

Bitmap bitmap = QrCode.generateQRCode(data);
qrcode.setImageBitmap(bitmap);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//qrcode 가 없으면
if (result.getContents() == null) {
Toast.makeText(MainActivity.this, "취소!", Toast.LENGTH_SHORT).show();
} else {
//qrcode 결과가 있으면
Toast.makeText(MainActivity.this, "스캔완료!", Toast.LENGTH_SHORT).show();
mResult.setText(result.getContents());
}

} else {
super.onActivityResult(requestCode, resultCode, data);
}
}




반응형

'Android' 카테고리의 다른 글

Lock Pattern  (0) 2018.11.06
FingerPrint(지문인증)  (0) 2018.11.06
NFC  (0) 2018.10.22
안드로이드 실행환경  (0) 2018.10.19
가로 막대 프로그레스바  (0) 2018.10.15