투케이2K

561. (Android/Java) [유틸 파일] observableQrImage : QR (큐알) ImageView 팝업창 호출 수행 본문

Android

561. (Android/Java) [유틸 파일] observableQrImage : QR (큐알) ImageView 팝업창 호출 수행

투케이2K 2023. 5. 17. 19:34

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java

 

[소스 코드]

 

    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : [Observable] : [QR ImageView 팝업창 호출 처리 메소드]
    // -----------------------------------------------------------------------------------------
    // TODO [호출 방법 소스 코드]
    // -----------------------------------------------------------------------------------------
    /*
    try {

        // [팝업창 활성 수행]
        C_Ui_View.observableQrImage(A_Intro.this, "알림", "hello", "확인")
                .subscribeOn(AndroidSchedulers.mainThread()) // [Observable (생성자) 로직을 IO 스레드에서 실행 : 백그라운드]
                .observeOn(Schedulers.io()) // [Observer (관찰자) 로직을 메인 스레드에서 실행]
                .subscribe(new Observer<String>() { // [Observable.create 타입 지정]
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }
                    @Override
                    public void onNext(@NonNull String value) {
                        S_Log.ltw("================================================");
                        S_Log.cnt("["+ACTIVITY_NAME+" >> QR ImageView 팝업창 활성 :: onNext]");
                        S_Log.cnt("-----------------------------------------");
                        S_Log.cnt("[VALUE :: "+String.valueOf(value)+"]");
                        S_Log.lbw("================================================");
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        S_Log.lte("================================================");
                        S_Log.cnt("["+ACTIVITY_NAME+" >> QR ImageView 팝업창 활성 :: onError]");
                        S_Log.cnt("-----------------------------------------");
                        S_Log.cnt("[ERROR :: "+String.valueOf(e.getMessage())+"]");
                        S_Log.lbe("================================================");
                    }
                    @Override
                    public void onComplete() {
                    }
                });


    }
    catch (Exception e){
        e.printStackTrace();
    }
    */
    // -----------------------------------------------------------------------------------------
    public static Observable<String> observableQrImage(Context mContext, String title, String qrData, String ok){

        // [로직 처리 실시]
        return Observable.create(subscriber -> {

            /**
             * ------------------------------------------------------------------
             * [의존성 부여 필요]
             * ------------------------------------------------------------------
             * implementation 'com.github.kenglxn.QRGen:android:2.6.0'
             * implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
             * implementation "com.google.zxing:core:3.3.0"
             * ------------------------------------------------------------------
             * */

            try {
                // ===============================================================
                S_Log._D_("QR 표시 ImageView 팝업창 호출 수행 실시", null);
                // ===============================================================


                // [비트맵 생성 실시]
                Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                DisplayMetrics outMetrics = new DisplayMetrics();
                display.getMetrics(outMetrics);
                float density = mContext.getResources().getDisplayMetrics().density;

                float qrWidth = outMetrics.widthPixels; // TODO [QR 생성 데이터]

                float dpHeight = outMetrics.heightPixels / density; // TODO [팝업창 사이즈]
                float dpWidth = outMetrics.widthPixels / density; // TODO [팝업창 사이즈]

                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                BitMatrix bitMatrix = multiFormatWriter.encode(qrData, BarcodeFormat.QR_CODE,(int)qrWidth,(int)qrWidth);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);


                // [UI 생성 실시]
                final ImageView imageView = new ImageView( mContext );
                //imageView.setPadding(5,5,5,5);


                // [이미지 뷰에 비트맵 지정]
                imageView.setImageBitmap(bitmap);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);


                // [팝업창 생성 실시]
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (mContext != null){

                            // [AlertDialog 팝업창 생성]
                            AlertDialog dialog = new AlertDialog.Builder(mContext)
                                    .setTitle(title) //[팝업창 타이틀 지정]
                                    //.setIcon(R.drawable.app_icon) //[팝업창 아이콘 지정]
                                    //.setMessage("\n") //[팝업창 내용 지정]
                                    .setView( imageView )
                                    .setCancelable(false) //[외부 레이아웃 클릭시도 팝업창이 사라지지않게 설정]
                                    .setPositiveButton(ok, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            // TODO Auto-generated method stub
                                            // -----------------------------------------
                                            // TODO [확인 버튼 클릭 이벤트 처리]
                                            // -----------------------------------------
                                            S_Log._D_("QR 표시 ImageView 팝업창 호출 [확인] 클릭", null);
                                            // -----------------------------------------


                                            // -----------------------------------------
                                            // TODO [리턴 데이터 반환]
                                            // -----------------------------------------
                                            try {
                                                subscriber.onNext(ok);
                                                subscriber.onComplete();
                                            }
                                            catch (Exception ex){
                                                ex.printStackTrace();
                                            }
                                            // -----------------------------------------
                                        }
                                    })
                                    .show();


                            // [팝업창 레이아웃 사이즈 변경]
                            //*
                            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                            Window window = dialog.getWindow();
                            lp.copyFrom(window.getAttributes());

                            //lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                            //lp.height = WindowManager.LayoutParams.MATCH_PARENT;

                            //lp.width =  (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 350, mContext.getResources().getDisplayMetrics());
                            //lp.height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 350, mContext.getResources().getDisplayMetrics());

                            int dpWidthValue = (int)dpWidth;
                            int dpHeightValue = (int)dpHeight;

                            // ===============================================================
                            S_Log._W_("QR 표시 ImageView 팝업창 사이즈 확인", new String[]{"dpWidthValue :: " + String.valueOf(dpWidthValue)});
                            // ===============================================================

                            int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpWidthValue, mContext.getResources().getDisplayMetrics());
                            int height = width + (width/3);

                            lp.width =  width;
                            lp.height = height;

                            window.setAttributes(lp);
                            // */

                        }

                    }
                }, 0);

            } catch (final Exception e){
                e.printStackTrace();

                // ------------------------------------------------------
                // TODO [리턴 데이터 반환]
                // ------------------------------------------------------
                try {
                    subscriber.onNext("");
                    subscriber.onComplete();
                }
                catch (Exception ex){
                    ex.printStackTrace();
                }
            }

        });
    }

 

반응형
Comments