반응형
test device : note 4
android os : Marshmallow 6.0
build.gradle
1 2 3 4 | implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.journeyapps:zxing-android-embedded:3.6.0' |
activity_barcode_print.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".BarcodePrintActivity" > < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < androidx.appcompat.widget.Toolbar android:id = "@+id/tbToolBar" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:background = "?attr/colorPrimary" android:minHeight = "?attr/actionBarSize" android:theme = "?attr/actionBarTheme" app:title = "Barcode Print" /> < ImageView android:id = "@+id/ivBarcode" android:layout_width = "match_parent" android:layout_height = "150dp" /> < EditText android:id = "@+id/etBarcode" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "bardcode1234" /> < Button android:id = "@+id/btnCreate" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Barcode Create" android:onClick = "btnBarcodeCreateOnClick" /> </ LinearLayout > </ androidx.constraintlayout.widget.ConstraintLayout > |
BarcodePrintActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | package com.example.sampleproject; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import com.example.sampleproject.Norang.NorangActivity; 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.lang.String; import java.util.EnumMap; import java.util.Map; public class BarcodePrintActivity extends NorangActivity { private ImageView ivBarcode; private EditText etBarcode; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_barcode_print); InitializeControl(); InitializeCommonDataBind(); } private void InitializeCommonDataBind() { } private void InitializeControl() { toolbar = findViewById(R.id.tbToolBar); ivBarcode = findViewById(R.id.ivBarcode); etBarcode = findViewById(R.id.etBarcode); setToolbar( "" ); } public void btnBarcodeCreateOnClick(View view) { String barcodeText = etBarcode.getText().toString(); try { Bitmap bitmap = CreateBarcode(barcodeText, BarcodeFormat.CODE_128 , 500 , 100 ); ivBarcode.setImageBitmap(bitmap); } catch (WriterException e) { Log.e( "btnBarcodeCreateOnClick" , e.getMessage()); } } private Bitmap CreateBarcode(String barcodeText, BarcodeFormat barcodeFormat, int imageWidth, int imageHeight) throws WriterException { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); BitMatrix bitMatrix; Map<EncodeHintType, Object> map = new EnumMap<EncodeHintType, Object>(EncodeHintType. class ); int width; int height; int [] pixels; if (barcodeText == null ) return null ; map.put(EncodeHintType.CHARACTER_SET, "UTF-8" ); try { bitMatrix = multiFormatWriter.encode(barcodeText, barcodeFormat, imageWidth, imageHeight, map); } catch (IllegalArgumentException e) { Log.e( "CreateBarcode" , e.getMessage()); return null ; } width = bitMatrix.getWidth(); height = bitMatrix.getHeight(); pixels = new int [width * height]; for ( int i = 0 ; i < height; i++) { for ( int j = 0 ; j < width; j++) pixels[(i * width) + j] = bitMatrix.get(j, i) ? Color.BLACK : Color.WHITE; } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //barcode bitmap.setPixels(pixels, 0 , width, 0 , 0 , width, height); return bitmap; } } |
반응형
'Android' 카테고리의 다른 글
Android Dialog (3) | 2021.04.02 |
---|---|
Android QRcode Create (2) | 2021.03.17 |
Bluetooth file & text transfer #3 client (4) | 2021.03.17 |
Bluetooth file & text transfer #2 server (4) | 2021.03.17 |
Bluetooth file & text transfer #1 etc (6) | 2021.03.17 |
댓글