Skip to content

Commit

Permalink
优化了一些api
Browse files Browse the repository at this point in the history
  • Loading branch information
devilsen committed Jul 21, 2019
1 parent c6f1f10 commit 7fd9032
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 15 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,59 @@ C++ port of ZXing for Android

加入了OpenCV的图像处理,能在更远的距离进行放大操作,并且能更快的识别出二维码。

### 使用
在gradle中:
``` groovy
implementation 'me.devilsen:CZXing:0.1.1'
implementation 'me.devilsen:CZXing:0.2'
```

#### 1. 直接使用
你可以直接使用已经封装好的ScanActivity作为扫码界面
```java
Intent intent = new Intent(this, ScanActivity.class);
startActivity(intent);
```

使用ScanActivity来接管扫描返回的数据
```java
ScanActivityDelegate.getInstance().setScanResultDelegate(result -> {
Intent intent = new Intent(MainActivity.this, DelegateActivity.class);
intent.putExtra("result", result);
startActivity(intent);
});
```
#### 2. 自定义界面
或者使用ScanView来自定义你的界面
```xml
<me.devilsen.czxing.view.ScanView
android:id="@+id/surface_view_scan"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```

在自定义的Activity中你需要接管ScanView的生命周期,具体可以参看ScanActivity界面,同时设定setScanListener()
```java
mScanView.setScanListener(new ScanListener() {
@Override
public void onScanSuccess(String result) {
// 扫码成功
}

@Override
public void onOpenCameraError() {
// 打开相机出错
}

@Override
public void onClickCard() {
// 点击我的卡片
}
});
```

如果你需要展示 我的卡片 可以选择关闭
```java
mScanView.hideCard();
```

### 效果展示
Expand Down
2 changes: 1 addition & 1 deletion czxing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "0.1"
versionName "0.2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
12 changes: 8 additions & 4 deletions czxing/src/main/java/me/devilsen/czxing/ScanActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@
public class ScanActivity extends AppCompatActivity implements ScanListener {

private ScanView mScanView;
private ScanActivityDelegate.OnScanDelegate scanDelegate;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_scan);

BarCodeUtil.setDebug(true);
BarCodeUtil.setDebug(false);
ScreenUtil.setFullScreen(this);

mScanView = findViewById(R.id.surface_view_scan);
mScanView.setScanListener(this);
mScanView.hideCard();

scanDelegate = ScanActivityDelegate.getInstance().getScanDelegate();
}

@Override
Expand All @@ -62,7 +66,6 @@ protected void onDestroy() {
public void onScanSuccess(String result) {
BarCodeUtil.d(result);

ScanActivityDelegate.OnScanDelegate scanDelegate = ScanActivityDelegate.getInstance().getScanDelegate();
if (scanDelegate != null) {
scanDelegate.onScanResult(result);
} else {
Expand All @@ -80,8 +83,9 @@ public void onOpenCameraError() {

@Override
public void onClickCard() {

if (scanDelegate != null) {
scanDelegate.onClickCard();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ private void restParams() {

/**
* 对焦是否被锁定
*
* @return
*/
public boolean isFocusLocked() {
if (canFocus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public OnScanDelegate getScanDelegate() {

public interface OnScanDelegate {
void onScanResult(String result);

void onClickCard();
}

}
13 changes: 13 additions & 0 deletions czxing/src/main/java/me/devilsen/czxing/view/ScanBoxView.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class ScanBoxView extends View {
private ScanBoxClickListener mFlashLightListener;
// 是否处于黑暗环境
private boolean isDark;
private boolean mDrawCardText = true;

public ScanBoxView(Context context) {
this(context, null);
Expand Down Expand Up @@ -274,6 +275,11 @@ private void drawTipText(Canvas canvas) {
mFramingRect.bottom + mTextSize * 2,
mTxtPaint);

// 隐藏 我的卡片 文字
if (!mDrawCardText){
return;
}

mTxtPaint.setTextSize(mTextSizeBig);
mTxtPaint.setColor(mTextColorBig);
String clickText = "我的名片";
Expand Down Expand Up @@ -337,6 +343,13 @@ public void setDark(boolean dark) {
isDark = dark;
}

/**
* 隐藏 我的卡片 功能
*/
public void hideCardText() {
this.mDrawCardText = false;
}

public interface ScanBoxClickListener {
void onFlashLightClick();

Expand Down
4 changes: 4 additions & 0 deletions czxing/src/main/java/me/devilsen/czxing/view/ScanView.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ public void onCardTextClick() {
mScanListener.onClickCard();
}
}

public void hideCard() {
mScanBoxView.hideCardText();
}
}
2 changes: 1 addition & 1 deletion sample/src/main/java/me/sam/czxing/DelegateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_delegate);
String result = getIntent().getStringExtra("result");

TextView resultTxt = findViewById(me.devilsen.czxing.R.id.text_view_scan_result);
TextView resultTxt = findViewById(R.id.text_view_delegate_result);
resultTxt.setText(result);
}
}
19 changes: 14 additions & 5 deletions sample/src/main/java/me/sam/czxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ protected void onCreate(Bundle savedInstanceState) {

requestPermission();

// ScanActivityDelegate.getInstance().setScanResultDelegate(result -> {
// Intent intent = new Intent(MainActivity.this, DelegateActivity.class);
// intent.putExtra("result", result);
// startActivity(intent);
// });
ScanActivityDelegate.getInstance().setScanResultDelegate(new ScanActivityDelegate.OnScanDelegate() {
@Override
public void onScanResult(String result) {
Intent intent = new Intent(MainActivity.this, DelegateActivity.class);
intent.putExtra("result", result);
startActivity(intent);
}

@Override
public void onClickCard() {
Intent intent = new Intent(MainActivity.this, MyCardActivity.class);
startActivity(intent);
}
});
}

public void scan(View view) {
Expand Down
9 changes: 8 additions & 1 deletion sample/src/main/res/layout/activity_my_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:src="@mipmap/ic_launcher" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Devilsen"/>

</LinearLayout>

0 comments on commit 7fd9032

Please sign in to comment.