QR코드 생성
2016. 4. 25. 15:49ㆍAndroid
반응형
ㅇ build.gradle
compile 'com.journeyapps:zxing-android-embedded:3.0.0@aar'
compile 'com.google.zxing:core:3.2.0'
compile 'com.google.zxing:javase:2.2'public class CreateQRCode {
private String account;
private String name;
private String amount;
public CreateQRCode(){
}
public CreateQRCode(String account, String name, String amount){
this.account = account;
this.name = name;
this.amount = amount;
}
public void create(){
ArrayList<String> list = new ArrayList<String>();
list.add(this.account);
list.add(this.name);
list.add(this.amount);
String content = getContents(list).toString();
// content UTF-8 변환
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
File dirs = new File(Environment.getExternalStorageDirectory(), "/Pictures/QRCode");
OutputStream outStream = null;
try {
if (!dirs.exists()) { // 원하는 경로에 폴더가 있는지 확인
dirs.mkdirs(); // 폴더 생성
}
/**
* 생성할 QR코드의 저장 경로 및 파일명(확장자 포함) 지정
*/
String ex_storage = Environment.getExternalStorageDirectory().getAbsolutePath();// Get Absolute Path in External Sdcard
String folder_name = "/Pictures/QRCode/"; // Set folder in absolute path
String path = ex_storage+folder_name;
String fileName = String.valueOf(getDateTime())+".jpg";
outStream = new FileOutputStream(path+fileName);
int height = bitMatrix.getHeight();
int width = bitMatrix.getWidth();
/**
* bitmap으로 QR코드 생성
*/
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++){
bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (WriterException e) {
Log.d("WriterException ",e.getMessage());
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
/**
* QR코드에 입력할 내용을 콤마로 구분
* @param list
* @return
*/
private static StringBuilder getContents(ArrayList<String> list){
StringBuilder sb = new StringBuilder();
for(int i=0; i<list.size(); i++){
sb.append(list.get(i));
if(i != (list.size()-1))
sb.append(",");
}
return sb;
}
/**
* QR코드 bitmap 파일 이름
* @return
*/
private static StringBuilder getDateTime(){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int date = cal.get(Calendar.DATE);
//
// String time = String.valueOf(System.currentTimeMillis()).substring(0,6);
long sysTime = System.currentTimeMillis();
SimpleDateFormat dayTime = new SimpleDateFormat("hhmmss");
String time = dayTime.format(new Date(sysTime));
StringBuilder sb = new StringBuilder();
sb.append(year).append(month).append(date).append(time);
return sb;
}
}
반응형
'Android' 카테고리의 다른 글
Android 6.0 마시멜로 대응(런타임 권한 모델) (0) | 2016.04.26 |
---|---|
QR코드 스캔 (0) | 2016.04.25 |
@SuppressLint (0) | 2016.04.19 |
SMS 인증번호 받기 (0) | 2016.04.19 |
웹뷰(Web View) (0) | 2016.04.18 |