android:update scan qrcode lib

This commit is contained in:
刘涛 2020-05-08 19:53:11 +08:00 committed by osborn
parent c51fc83537
commit 44286bcb28
6 changed files with 35 additions and 108 deletions

View File

@ -32,11 +32,12 @@ dependencies {
implementation "com.google.android.material:material:1.0.0" implementation "com.google.android.material:material:1.0.0"
implementation 'com.squareup.okhttp3:okhttp:3.12.1' implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.google.code.gson:gson:2.8.5'
implementation 'cn.bingoogolapple:bga-qrcode-zbar:1.3.7'
api 'org.greenrobot:eventbus:3.1.1' api 'org.greenrobot:eventbus:3.1.1'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'cn.yipianfengye.android:zxing-library:2.2'
} }
apply from: rootProject.file('scripts/upload.gradle') apply from: rootProject.file('scripts/upload.gradle')

View File

@ -1,12 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pub.doric.devkit"> package="pub.doric.devkit">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<application> <application>
<activity android:name=".ui.DoricDevActivity" /> <activity android:name=".ui.DoricDevActivity" />
<activity android:name=".ui.ScanQRCodeActivity" />
</application> </application>
</manifest> </manifest>

View File

@ -53,7 +53,7 @@ public class WSClient extends WebSocketListener {
} }
public void close() { public void close() {
webSocket.close(1024, "Close"); webSocket.close(1000, "Close");
} }
@Override @Override
@ -97,6 +97,7 @@ public class WSClient extends WebSocketListener {
@Override @Override
public void onClosed(WebSocket webSocket, int code, String reason) { public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason); super.onClosed(webSocket, code, reason);
EventBus.getDefault().post(new ConnectExceptionEvent());
} }
@Override @Override

View File

@ -9,6 +9,7 @@ import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -17,6 +18,9 @@ import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -34,11 +38,12 @@ import pub.doric.devkit.event.OpenEvent;
import pub.doric.devkit.event.StartDebugEvent; import pub.doric.devkit.event.StartDebugEvent;
public class DoricDevActivity extends AppCompatActivity { public class DoricDevActivity extends AppCompatActivity {
private int REQUEST_CODE=100;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
ZXingLibrary.initDisplayOpinion(this);
setContentView(R.layout.layout_debug_context); setContentView(R.layout.layout_debug_context);
initDisconnect(); initDisconnect();
if (DoricDev.getInstance().isInDevMode()) { if (DoricDev.getInstance().isInDevMode()) {
@ -57,8 +62,8 @@ public class DoricDevActivity extends AppCompatActivity {
new String[]{Manifest.permission.CAMERA,}, 1); new String[]{Manifest.permission.CAMERA,}, 1);
} }
} else { } else {
Intent intent = new Intent(DoricDevActivity.this, ScanQRCodeActivity.class); Intent intent = new Intent(DoricDevActivity.this, CaptureActivity.class);
startActivity(intent); startActivityForResult(intent, REQUEST_CODE);
} }
} }
} }
@ -71,13 +76,30 @@ public class DoricDevActivity extends AppCompatActivity {
if (requestCode == 1) { if (requestCode == 1) {
for (int i = 0; i < permissions.length; i++) { for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(DoricDevActivity.this, ScanQRCodeActivity.class); Intent intent = new Intent(DoricDevActivity.this, CaptureActivity.class);
startActivity(intent); startActivityForResult(intent, REQUEST_CODE);
} }
} }
} }
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (null != data) {
Bundle bundle = data.getExtras();
if (bundle == null) {
return;
}
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
String result = bundle.getString(CodeUtils.RESULT_STRING);
DevKit.ip = result;
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
DevKit.getInstance().connectDevKit("ws://" + result + ":7777");
}
}
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -107,7 +129,9 @@ public class DoricDevActivity extends AppCompatActivity {
button.setOnClickListener(new View.OnClickListener() { button.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if(DoricDev.getInstance().isInDevMode()){
DoricDev.getInstance().closeDevMode(); DoricDev.getInstance().closeDevMode();
}
finish(); finish();
} }
}); });

View File

@ -1,76 +0,0 @@
package pub.doric.devkit.ui;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;
import pub.doric.devkit.DevKit;
import pub.doric.devkit.R;
public class ScanQRCodeActivity extends AppCompatActivity implements QRCodeView.Delegate {
private ZBarView mZbarView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scan_qrcode);
mZbarView = findViewById(R.id.zbar_view);
mZbarView.setDelegate(this);
}
@Override
protected void onStart() {
super.onStart();
mZbarView.startCamera();
mZbarView.startSpotAndShowRect();
}
@Override
protected void onStop() {
super.onStop();
mZbarView.stopCamera();
}
@Override
protected void onDestroy() {
mZbarView.onDestroy();
super.onDestroy();
}
@Override
public void onScanQRCodeSuccess(String result) {
setTitle("扫描结果为:" + result);
DevKit.ip = result;
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
DevKit.getInstance().connectDevKit("ws://" + result + ":7777");
finish();
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {
String tipText = mZbarView.getScanBoxView().getTipText();
String ambientBrightnessTip = "\n环境过暗请打开闪光灯";
if (isDark) {
if (!tipText.contains(ambientBrightnessTip)) {
mZbarView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
}
} else {
if (tipText.contains(ambientBrightnessTip)) {
tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
mZbarView.getScanBoxView().setTipText(tipText);
}
}
}
@Override
public void onScanQRCodeOpenCameraError() {
System.out.println();
}
}

View File

@ -1,22 +0,0 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<cn.bingoogolapple.qrcode.zbar.ZBarView
android:id="@+id/zbar_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:qrcv_animTime="1000"
app:qrcv_borderColor="@android:color/white"
app:qrcv_borderSize="1dp"
app:qrcv_cornerColor="@android:color/white"
app:qrcv_cornerLength="20dp"
app:qrcv_cornerSize="3dp"
app:qrcv_isShowDefaultScanLineDrawable="true"
app:qrcv_maskColor="#33FFFFFF"
app:qrcv_rectWidth="200dp"
app:qrcv_scanLineColor="@android:color/white"
app:qrcv_topOffset="90dp" />
</LinearLayout>