增加扫码等功能

This commit is contained in:
cebgcontract 2022-11-08 19:08:34 +08:00
parent 0b5ed5a03e
commit 099746292a
15 changed files with 197 additions and 27 deletions

3
.idea/misc.xml generated
View File

@ -3,9 +3,12 @@
<component name="DesignSurface"> <component name="DesignSurface">
<option name="filePathToZoomLevelMap"> <option name="filePathToZoomLevelMap">
<map> <map>
<entry key="../../../../.gradle/caches/transforms-2/files-2.1/1d876133b5a2bd3ea40b131cf9d28edb/jetified-zxing-lite-2.1.1/res/layout/zxl_capture.xml" value="0.14322916666666666" />
<entry key="app/src/main/res/layout/custom_capture_activity.xml" value="0.18840579710144928" />
<entry key="app/src/main/res/layout/qr_vew.xml" value="0.125" /> <entry key="app/src/main/res/layout/qr_vew.xml" value="0.125" />
<entry key="app/src/main/res/layout/qrcode_view.xml" value="0.1421875" /> <entry key="app/src/main/res/layout/qrcode_view.xml" value="0.1421875" />
<entry key="res/drawable/ic_launcher_background.xml" value="0.225" /> <entry key="res/drawable/ic_launcher_background.xml" value="0.225" />
<entry key="res/drawable/qr_background.xml" value="0.153" />
<entry key="res/layout/activity_main.xml" value="0.18840579710144928" /> <entry key="res/layout/activity_main.xml" value="0.18840579710144928" />
</map> </map>
</option> </option>

View File

@ -29,6 +29,9 @@
<activity <activity
android:name="com.king.zxing.CaptureActivity" android:name="com.king.zxing.CaptureActivity"
android:theme="@style/CaptureTheme"/> android:theme="@style/CaptureTheme"/>
<activity
android:name=".activity.CustomCaptureActivity"
android:theme="@style/CaptureTheme"/>
<!-- <!--
This activity declaration is merged with the version from the library manifest. This activity declaration is merged with the version from the library manifest.
It demonstrates how an https redirect can be captured, in addition to or instead of It demonstrates how an https redirect can be captured, in addition to or instead of

View File

@ -138,4 +138,5 @@ dependencies {
implementation "com.squareup.okio:okio:2.10.0" implementation "com.squareup.okio:okio:2.10.0"
implementation 'com.android.volley:volley:1.2.1' implementation 'com.android.volley:volley:1.2.1'
implementation 'org.greenrobot:eventbus:3.0.0' implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
} }

View File

@ -16,6 +16,7 @@ import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.Window; import android.view.Window;
import com.fitchgc.jcwallet.activity.CustomCaptureActivity;
import com.fitchgc.jcwallet.dialog.QRCodeActivity; import com.fitchgc.jcwallet.dialog.QRCodeActivity;
import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
@ -29,7 +30,6 @@ import com.jc.jcfw.JcSDK;
import com.jc.jcfw.appauth.AuthStateManager; import com.jc.jcfw.appauth.AuthStateManager;
import com.jc.jcfw.appauth.JConfiguration; import com.jc.jcfw.appauth.JConfiguration;
import com.king.zxing.CameraScan; import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
import com.king.zxing.util.CodeUtils; import com.king.zxing.util.CodeUtils;
import com.king.zxing.util.LogUtils; import com.king.zxing.util.LogUtils;
import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayer;
@ -161,8 +161,7 @@ public class MainActivity extends Activity
case REQUEST_CODE_SCAN: case REQUEST_CODE_SCAN:
String result = CameraScan.parseScanResult(data); String result = CameraScan.parseScanResult(data);
Log.i(TAG, "scan qrcode with funId: " +funId+ " result: " + result); Log.i(TAG, "scan qrcode with funId: " +funId+ " result: " + result);
String jsonStr = String.format("{errcode: 0, data: '%s'}", result); JcSDK.scanQrCb(funId, null, result);
JcSDK.csCallback(funId, jsonStr);
funId = ""; funId = "";
break; break;
case REQUEST_CODE_PHOTO: case REQUEST_CODE_PHOTO:
@ -191,8 +190,21 @@ public class MainActivity extends Activity
break; break;
} }
} else { } else {
// JcSDK.csCallback(funId, "{errcode: 0, errmsg: 'cancel'}"); boolean next = false;
funId = ""; if (requestCode == REQUEST_CODE_SCAN && data != null) {
if (data.getBooleanExtra("localImg", false)) {
startPhotoCode(this.funId);
next = true;
}
}
if (!next) {
// JcSDK.csCallback(funId, "{errcode: 0, errmsg: 'cancel'}");
if (requestCode == REQUEST_CODE_SCAN) {
JcSDK.scanQrCb(funId, "activity result with code: " + resultCode, null);
}
funId = "";
}
} }
} }
@ -345,7 +357,6 @@ public class MainActivity extends Activity
@Override @Override
public void onPermissionsDenied(int requestCode, List<String> list) { public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied // Some permissions have been denied
// ...
if (requestCode ==RC_CAMERA && null != funId && !"".equals(funId)) { if (requestCode ==RC_CAMERA && null != funId && !"".equals(funId)) {
JcSDK.csCallback(funId, "{errcode: 1, errmsg: 'no camera permission'}"); JcSDK.csCallback(funId, "{errcode: 1, errmsg: 'no camera permission'}");
funId = ""; funId = "";
@ -356,7 +367,7 @@ public class MainActivity extends Activity
// begin of qrcode // begin of qrcode
/** /**
* 检测拍摄权限 * check if had permission for camera
*/ */
@AfterPermissionGranted(RC_CAMERA) @AfterPermissionGranted(RC_CAMERA)
private void checkCameraPermissions(){ private void checkCameraPermissions(){
@ -375,11 +386,14 @@ public class MainActivity extends Activity
*/ */
private void startScan(String title){ private void startScan(String title){
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this,R.anim.in,R.anim.out); ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(this,R.anim.in,R.anim.out);
Intent intent = new Intent(this, CaptureActivity.class); Intent intent = new Intent(this, CustomCaptureActivity.class);
// Intent intent = new Intent(this, CaptureActivity.class);
intent.putExtra(KEY_TITLE, title); intent.putExtra(KEY_TITLE, title);
intent.putExtra(KEY_IS_CONTINUOUS, false); intent.putExtra(KEY_IS_CONTINUOUS, false);
this.startActivityForResult(intent, REQUEST_CODE_SCAN, optionsCompat.toBundle()); runOnUiThread(() -> {
// ActivityCompat.startActivityForResult(this, intent, REQUEST_CODE_SCAN, optionsCompat.toBundle()); this.startActivityForResult(intent, REQUEST_CODE_SCAN, optionsCompat.toBundle());
});
} }
private void asyncThread(Runnable runnable){ private void asyncThread(Runnable runnable){
@ -390,16 +404,19 @@ public class MainActivity extends Activity
try { try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),data.getData()); Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),data.getData());
asyncThread(() -> { asyncThread(() -> {
final String result = CodeUtils.parseCode(bitmap); final String result = CodeUtils.parseQRCode(bitmap);
LogUtils.d("result:" + result); if (null == result || "".equals(result)) {
String jsonStr = String.format("{errcode: 0, data: '%s'}", result); JcSDK.scanQrCb(funId, "no qrdeata", null);
JcSDK.csCallback(funId, jsonStr); } else {
LogUtils.d("result:" + result);
JcSDK.scanQrCb(funId, null, result);
}
funId = ""; funId = "";
}); });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
JcSDK.csCallback(funId, String.format("{errcode: 1, errmsg: '%s'}", e.toString())); JcSDK.scanQrCb(funId, e.toString(), null);
funId = ""; funId = "";
} }
@ -411,6 +428,16 @@ public class MainActivity extends Activity
checkCameraPermissions(); checkCameraPermissions();
} }
/**
* start image scan
*/
public void startPhotoCode(String funId){
Intent pickIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, REQUEST_CODE_PHOTO);
}
// end of qrcode // end of qrcode
public void signWithGoogle(String funId) { public void signWithGoogle(String funId) {

View File

@ -0,0 +1,46 @@
package com.fitchgc.jcwallet.activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import com.fitchgc.jcwallet.R;
import com.king.zxing.CaptureActivity;
import static com.fitchgc.jcwallet.MainActivity.KEY_TITLE;
public class CustomCaptureActivity extends CaptureActivity {
private static final String TAG = CustomCaptureActivity.class.getSimpleName();
@Override
public int getLayoutId() {
return R.layout.custom_capture_activity;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent in = getIntent();
String title = in.getStringExtra(KEY_TITLE);
Button localBtn = findViewById(R.id.qrLocalBtn);
localBtn.setOnClickListener(v -> onClickLocalImg());
TextView titleLabel = findViewById(R.id.scanQrTitleLabel);
titleLabel.setText(title);
}
@Override
public void initCameraScan() {
super.initCameraScan();
getCameraScan()
.setPlayBeep(true)
.setVibrate(true);
}
protected void onClickLocalImg(){
Intent ins=new Intent();
ins.putExtra("localImg", true);
setResult(1, ins);
finish();
}
}

View File

@ -1,8 +1,8 @@
package com.fitchgc.jcwallet.dialog; package com.fitchgc.jcwallet.dialog;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -11,7 +11,7 @@ import com.fitchgc.jcwallet.MainActivity;
import com.fitchgc.jcwallet.R; import com.fitchgc.jcwallet.R;
import com.king.zxing.util.CodeUtils; import com.king.zxing.util.CodeUtils;
public class QRCodeActivity extends BaseDialog{ public class QRCodeActivity extends Dialog {
private final Context baseContent; private final Context baseContent;
private TextView titleLabel; private TextView titleLabel;
@ -19,7 +19,7 @@ public class QRCodeActivity extends BaseDialog{
public QRCodeActivity(Context context) { public QRCodeActivity(Context context) {
super(context); super(context, R.style.DialogStyle);
baseContent = context; baseContent = context;
} }
@ -33,8 +33,8 @@ public class QRCodeActivity extends BaseDialog{
public void showQRCode(String content, String title){ public void showQRCode(String content, String title){
new Thread(() -> { new Thread(() -> {
Bitmap logo = BitmapFactory.decodeResource(baseContent.getResources(),R.drawable.zxl_flashlight_on); // Bitmap logo = BitmapFactory.decodeResource(baseContent.getResources(),R.drawable.zxl_flashlight_on);
Bitmap bitmap = CodeUtils.createQRCode(content,600, logo); Bitmap bitmap = CodeUtils.createQRCode(content,600, null);
MainActivity.app.runOnUiThread(()->{ MainActivity.app.runOnUiThread(()->{
titleLabel.setText(title); titleLabel.setText(title);
ivCode.setImageBitmap(bitmap); ivCode.setImageBitmap(bitmap);

View File

@ -83,7 +83,6 @@ public class JcSDK {
public static void googleOauthCb(String funId, String error, String idToken) { public static void googleOauthCb(String funId, String error, String idToken) {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
try { try {
if (error != "" && null != error) { if (error != "" && null != error) {
result.put("errocde", 1); result.put("errocde", 1);
result.put("errmessage", error); result.put("errmessage", error);
@ -98,4 +97,22 @@ public class JcSDK {
String methodName = "jniCallback"; String methodName = "jniCallback";
JcSDK.runJS(funId, methodName, result.toString()); JcSDK.runJS(funId, methodName, result.toString());
} }
public static void scanQrCb(String funId, String error, String data) {
JSONObject result = new JSONObject();
try {
if (error != "" && null != error) {
result.put("errocde", 1);
result.put("errmessage", error);
} else {
result.put("errocde", 0);
result.put("data", data);
}
} catch (JSONException e) {
e.printStackTrace();
}
String methodName = "jniCallback";
JcSDK.runJS(funId, methodName, result.toString());
}
} }

View File

@ -0,0 +1,58 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView
android:id="@+id/viewfinderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:labelText="@string/tips_scan_code"
app:laserColor="@color/colorAccent"
app:frameColor="@color/colorPrimary"
app:cornerColor="@color/colorPrimary"
app:labelTextLocation="bottom"
app:laserStyle="grid" />
<ImageView
android:id="@+id/ivFlashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/flash_selected_selector"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="160dp" />
<TextView
android:id="@+id/scanQrTitleLabel"
android:text="sample label"
android:gravity="center_horizontal"
android:layout_marginTop="210dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button
android:id="@+id/qrLocalBtn"
android:text="Select Image Local"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="300dp"
android:textColor="@color/white"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@drawable/qr_background"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -15,7 +14,7 @@
/> />
<TextView <TextView
android:id="@+id/qrTitleLabel" android:id="@+id/qrTitleLabel"
android:text="假装是 APP 主页" android:text="sample label"
android:gravity="center" android:gravity="center"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>

BIN
res/drawable/flash_off.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
res/drawable/flash_on.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/flash_on"/>
<item android:state_selected="false" android:drawable="@drawable/flash_off"/>
</selector>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke
android:width="0.01dp"
android:color="#BFBFBF" />
</shape>

View File

@ -4,4 +4,5 @@
<string name="permission_camera">Scan QRCode need camera permission</string> <string name="permission_camera">Scan QRCode need camera permission</string>
<string name="qr_code_title">QRCode</string> <string name="qr_code_title">QRCode</string>
<string name="default_web_client_id">165555585193-j22geb66hjku5ns5lq5voaj4v7811cl7.apps.googleusercontent.com</string> <string name="default_web_client_id">165555585193-j22geb66hjku5ns5lq5voaj4v7811cl7.apps.googleusercontent.com</string>
<string name="tips_scan_code"></string>
</resources> </resources>

View File

@ -11,10 +11,10 @@
</style> </style>
<style name="DialogStyle" parent="@android:style/Theme.Dialog"> <style name="DialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item> <item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@color/transparent</item> <item name="android:windowBackground">@color/white</item>
<item name="android:windowNoTitle">true</item> <item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item> <item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item> <item name="android:windowContentOverlay">@color/white</item>
</style> </style>
<style name="Dialog_bottom_enter_ani"> <style name="Dialog_bottom_enter_ani">
<item name="android:windowEnterAnimation">@anim/dlg_enter</item> <item name="android:windowEnterAnimation">@anim/dlg_enter</item>