From afc0f02378fcf0e2acf734de1869731508c8fb86 Mon Sep 17 00:00:00 2001 From: it232115 Date: Fri, 12 Jan 2024 11:32:50 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E6=AF=8E=E3=81=AE?= =?UTF-8?q?=E3=83=89=E3=83=A1=E3=82=A4=E3=83=B3=E3=81=8B=E3=82=89QR?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E7=94=9F=E6=88=90=E3=81=97?= =?UTF-8?q?=E3=80=81=E3=83=97=E3=83=AA=E3=83=B3=E3=82=BF=E3=83=BC=E3=81=B8?= =?UTF-8?q?=E5=8D=B0=E5=88=B7=E6=8C=87=E7=A4=BA=E3=82=92=E5=87=BA=E3=81=99?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/childguard/QrPrintFragment.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 app/src/main/java/com/example/childguard/QrPrintFragment.java diff --git a/app/src/main/java/com/example/childguard/QrPrintFragment.java b/app/src/main/java/com/example/childguard/QrPrintFragment.java new file mode 100644 index 0000000..d31ac6e --- /dev/null +++ b/app/src/main/java/com/example/childguard/QrPrintFragment.java @@ -0,0 +1,119 @@ +package com.example.childguard; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.drawable.BitmapDrawable; +import android.os.Bundle; + +import androidx.fragment.app.Fragment; +import androidx.print.PrintHelper; + +import android.util.AndroidRuntimeException; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.WriterException; +import com.journeyapps.barcodescanner.BarcodeEncoder; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link QrPrintFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class QrPrintFragment extends Fragment { + + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + // TODO: Rename and change types of parameters + private String mParam1; + private String mParam2; + + public QrPrintFragment() { + // Required empty public constructor + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param param1 Parameter 1. + * @param param2 Parameter 2. + * @return A new instance of fragment QrPrintFragment. + */ + // TODO: Rename and change types and number of parameters + public static QrPrintFragment newInstance(String param1, String param2) { + QrPrintFragment fragment = new QrPrintFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view=inflater.inflate(R.layout.fragment_qr_print, container, false); + + //固定のドメイン + String KoteiURL = "https://practicefirestore1-8808c.web.app/?id="; + //User毎のドメイン + String userURL="YKjFsZgJBlZmcyvdZ3Ap"; + //二つのドメインを合成する + String AllURL=KoteiURL+userURL; + + int size = 2500; + ImageView imageViewQrCode; + Bitmap QRGazou; + try { + //QRコード生成 + BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); + Bitmap bitmapqr = barcodeEncoder.encodeBitmap(AllURL, BarcodeFormat.QR_CODE, size, size); + imageViewQrCode = (ImageView) view.findViewById(R.id.qr_view); + imageViewQrCode.setTranslationX(1000); + imageViewQrCode.setTranslationY(1000); + imageViewQrCode.setImageBitmap(bitmapqr); + } catch (WriterException e) { + throw new AndroidRuntimeException("Barcode Error.", e); + } + // 画像合成の準備 + Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a_group_qr_sos); + Bitmap bitmap1 = ((BitmapDrawable) imageViewQrCode.getDrawable()).getBitmap(); + int width = bitmap.getWidth(); // 元ファイルの幅取得 + int height = bitmap.getHeight(); // 元ファイルの高さ取得 + QRGazou = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + //Canvasの準備 + Canvas canvas = new Canvas(QRGazou); + //画像のサイズの調整 + int disWidth = (width - bitmap1.getWidth()) / 2; + int disHeight = (int) ((height - bitmap1.getHeight()) / 1.3); + canvas.drawBitmap(bitmap, 0, 0, (Paint) null); // image, x座標, y座標, Paintインスタンス + canvas.drawBitmap(bitmap1, disWidth, disHeight, (Paint) null); // 画像合成 + //Androidからプリンターへ印刷指示を出すサポートライブラリ + PrintHelper printHelper = new PrintHelper(getActivity()); + printHelper.setColorMode(PrintHelper.COLOR_MODE_COLOR); + printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); + printHelper.printBitmap("job_name", QRGazou); + + return view; + } +} \ No newline at end of file