Merge pull request #9 from child-guardian/QR

Qr
This commit is contained in:
ろむねこ 2024-01-12 11:36:57 +09:00 committed by GitHub
commit 6759b2c831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 181 additions and 16 deletions

View File

@ -34,7 +34,6 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".UrlPageActivity" />
</application> </application>
</manifest> </manifest>

View File

@ -1,7 +1,12 @@
package com.example.childguard; package com.example.childguard;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
@ -9,15 +14,22 @@ import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.print.PrintHelper;
import android.util.AndroidRuntimeException;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.journeyapps.barcodescanner.BarcodeEncoder;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to * Use the {@link HomeFragment#newInstance} factory method to
@ -69,12 +81,20 @@ public class HomeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
//bluetooth設定ボタンの処理
View view=inflater.inflate(R.layout.fragment_home,container,false); View view=inflater.inflate(R.layout.fragment_home,container,false);
MainActivity activity = (MainActivity) getActivity(); MainActivity activity = (MainActivity) getActivity();
//QRコード印刷の処理
Button bt1=view.findViewById(R.id.Bluetooth_setup); Button bt1=view.findViewById(R.id.QRprinting);
bt1.setOnClickListener(new View.OnClickListener() { bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new QrPrintFragment());
}
});
//bluetooth設定ボタンの処理
Button bt2=view.findViewById(R.id.Bluetooth_setup);
bt2.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
replaceFragment(new bluetooth_setupFragment()); replaceFragment(new bluetooth_setupFragment());

View File

@ -74,22 +74,16 @@ public class QRFragment extends Fragment {
} }
//FragmentからActivityへデータの受け渡しをするためのInterface
// public interface OnDataPass{
// void onDataPass(String urlPass);
// }
private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(), private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
result -> { result -> {
//QRコードからデータを読み取れたかの確認 //result.getContents()でURLを入手
if(result.getContents() == null) { //読み取ったQRコードがChiled Guard用サイトのドメインを含むかの判定
Toast.makeText(getContext(), "Cancelled from fragment", Toast.LENGTH_LONG).show(); if(!((result.getContents()).contains("https://practicefirestore1-8808c.web.app/"))) {
Toast.makeText(getContext(), "Chiled Guardに対応するQRコードではありません", Toast.LENGTH_LONG).show();
} else { } else {
// dataPass.onDataPass(result.getContents()); //URLの表示
//画面遷移
Toast.makeText(getContext(), result.getContents(), Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), result.getContents(), Toast.LENGTH_SHORT).show();
// Intent intent=new Intent(getActivity(),UrlPageActivity.class); //ブラウザを起動しURL先のサイトを開く
// startActivity(intent);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build(); CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(requireContext(), Uri.parse(result.getContents())); customTabsIntent.launchUrl(requireContext(), Uri.parse(result.getContents()));

View File

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

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".QrPrintFragment"
android:weightSum="1">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="581dp"
android:layout_weight="1"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:srcCompat="@drawable/a_group_qr_sos"
android:layout_weight="0"/>
<ImageView
android:id="@+id/qr_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:srcCompat="@drawable/ic_launcher_foreground"
android:layout_weight="0"/>
</LinearLayout>