Contact Person

퐁당풍당

Mobile Developer Share

관리자 글쓰기 로그인

Mobile Developer Share

    카테고리

    • APP (25)
      • Android (11)
        • Develop (9)
        • Error (2)
      • IOS (9)
        • Develop (9)
        • Error (0)
      • - (2)
    Android/Develop

    [Android] 바코드 생성하기

    : 퐁당풍당
    : 2018. 3. 5. 16:04

    목적 : ZXing 라이브러리 사용하여 바코드 생성하기

    1. 라이브러리 추가
    compile 'com.google.zxing:core:3.3.2'

    2. 핵심 코드 

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

    private 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;
    }


    3. 사용법


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ImageView barcode = findViewById(R.id.imageView);

    String barcode_data = "1234567890";

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

    }



    도움이 되시면 좋겠습니다.


    저작자표시 동일조건

    'Android > Develop' 카테고리의 다른 글

    Android Card View Animation  (2) 2017.11.09
    Jenkins CI(continuous integration) for Android  (0) 2017.09.13
    RXJava, RxAndroid 시작!  (0) 2017.08.24
    모바일 웹 성능 관련 이슈 확인 및 검토 사항  (0) 2017.05.22
    갤럭시s8 , G6 리소스 대응!  (2) 2017.05.17
    퍼가기
    최근에 올라온 글
    RECENT POSTS
    최근에 달린 댓글
    RECENT COMMENTS
    LINK
    -
    방문자수
    COUNTER
    Today
    Yesterday
    Total
    퐁당풍당’s Blog is powered by / Designed by UX공작소
    위로가기

    티스토리툴바