QR코드를 생성하고 스캔하는 예제 포스팅입니다.
QR코드는 많은 분야에서 이용되고 있습니다. 최근 코로나시국으로 QR체크인은 일상입니다.
개인프로젝트 진행중 QR코드를 스캔해서 채팅방에 입장하는 기능을 추가했습니다.
zxing 라이브러리를 사용했습니다.
dependencies 에 아래코드를 추가합니다.
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
<?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=".CreateQr">
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button
android:id="@+id/btn_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="스캔"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<EditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="입력"
app:layout_constraintBottom_toTopOf="@+id/iv_qrCode"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_qrCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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=".ResultActivity">
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
생성버튼을 클릭하면 QR코드를 생성합니다.
스캔버튼을 클릭하고 QR코드를 스캔하면 EditText에서 입력 받은 값을 ResultActivity로 전달합니다.
public class CreateQr extends AppCompatActivity {
ImageView iv_qrCode;
Button btn_test, btn_scan;
EditText edt_text;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_create_qr);
mContext = CreateQr.this;
iv_qrCode = findViewById (R.id.iv_qrCode);
btn_test = findViewById (R.id.btn_test);
btn_scan = findViewById (R.id.btn_scan);
edt_text = findViewById (R.id.edt_text);
//QR코드를 스캔합니다.
btn_scan.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
new IntentIntegrator ((Activity) mContext).initiateScan ();
}
});
//클릭시 QR코드를 생성하고 다이얼로그에 띄웁니다.
//바코드인코더를 사용해 QR코드를 생성해서 비트맵이미지에 적용.
btn_test.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
AlertDialog.Builder ad = new AlertDialog.Builder (mContext);
ad.setTitle ("QR");
final ImageView iv = new ImageView (mContext); //qr코드를 띄울 이미지
String text = edt_text.getText ().toString ();
Hashtable hints = new Hashtable ();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter multiFormatWriter = new MultiFormatWriter ();
try {
BitMatrix bitMatrix = multiFormatWriter.encode (text, BarcodeFormat.QR_CODE, 1000, 1000, hints);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder ();
Bitmap bitmap = barcodeEncoder.createBitmap (bitMatrix);
iv.setImageBitmap (bitmap);
} catch (Exception e) {
}
ad.setView (iv);
ad.setPositiveButton ("ok", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss ();
}
});
ad.setNeutralButton ("center", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss ();
}
});
ad.setNegativeButton ("cancel", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss ();
}
});
ad.show ();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult (requestCode, resultCode, data);
if (result != null) {
if (result.getContents () == null) {
Toast.makeText (this, "전달할 값이 없습니다.", Toast.LENGTH_LONG).show ();
} else {
Toast.makeText (this, "Scanned: " + result.getContents (), Toast.LENGTH_LONG).show ();
Intent intent = new Intent (getApplicationContext (), ResultActivity.class);
intent.putExtra ("result", result.getContents ());
startActivity (intent);
}
} else {
super.onActivityResult (requestCode, resultCode, data);
}
}
}
전달 받은 값을 TextView에 띄웁니다.
public class ResultActivity extends AppCompatActivity {
TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_result);
Intent intent = getIntent ();
String result = intent.getStringExtra ("result");
tv_result=findViewById (R.id.tv_result);
tv_result.setText (result);
}
}