Skip to content

Commit

Permalink
优化回调方式,修复显示框拉升问题
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Jun 4, 2018
1 parent f945af1 commit 4a94a3e
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 48 deletions.
60 changes: 34 additions & 26 deletions app/src/main/java/com/maning/zxingcodedemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.MNScanManager;
import com.google.zxing.client.android.model.MNScanConfig;
import com.google.zxing.client.android.other.MNScanCallback;
import com.google.zxing.client.android.utils.ZXingUtils;

public class MainActivity extends AppCompatActivity {
Expand All @@ -39,7 +40,12 @@ protected void onCreate(Bundle savedInstanceState) {
}

public void scanCodeDefault(View view) {
MNScanManager.startScan(this, 1000);
MNScanManager.startScan(this, new MNScanCallback() {
@Override
public void onActivityResult(int resultCode, Intent data) {
handlerResult(resultCode, data);
}
});
}

public void scanCode(View view) {
Expand All @@ -57,7 +63,12 @@ public void scanCode(View view) {
//扫描线的颜色
.setScanColor("#FFFF00")
.builder();
MNScanManager.startScan(this, 1000, scanConfig);
MNScanManager.startScan(this, scanConfig, new MNScanCallback() {
@Override
public void onActivityResult(int resultCode, Intent data) {
handlerResult(resultCode, data);
}
});
}


Expand Down Expand Up @@ -85,31 +96,28 @@ public void createQRImage(View view) {

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000) {
if (data == null) {
return;
}
switch (resultCode) {
case CaptureActivity.RESULT_SUCCESS:
String resultSuccess = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_SUCCESS);
showToast(resultSuccess);
textView.setText(resultSuccess);
break;
case CaptureActivity.RESULT_FAIL:
String resultError = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_ERROR);
showToast(resultError);
break;
case CaptureActivity.RESULT_CANCLE:
showToast("取消扫码");
break;
}
}
}

private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}


private void handlerResult(int resultCode, Intent data) {
if (data == null) {
return;
}
switch (resultCode) {
case CaptureActivity.RESULT_SUCCESS:
String resultSuccess = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_SUCCESS);
showToast(resultSuccess);
textView.setText(resultSuccess);
break;
case CaptureActivity.RESULT_FAIL:
String resultError = data.getStringExtra(CaptureActivity.INTENT_KEY_RESULT_ERROR);
showToast(resultError);
break;
case CaptureActivity.RESULT_CANCLE:
showToast("取消扫码");
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void run() {
@Override
public void run() {
btn_dialog_bg.setVisibility(View.GONE);
Log.i(TAG, "图片的decodeQRCode:" + decodeQRCodeFromBitmap);
Log.i(TAG, "decodeQRCode:" + decodeQRCodeFromBitmap);
if (TextUtils.isEmpty(decodeQRCodeFromBitmap)) {
Toast.makeText(CaptureActivity.this, "未发现二维码", Toast.LENGTH_SHORT).show();
} else {
Expand Down Expand Up @@ -340,14 +340,14 @@ private void finishFailed(String errorMsg) {
this.setResult(RESULT_FAIL, intent);
this.finish();
//关闭窗体动画显示
this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out,0);
this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out, 0);
}

private void finishCancle() {
this.setResult(RESULT_CANCLE, null);
this.finish();
//关闭窗体动画显示
this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out,0);
this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out, 0);
}

private void finishSuccess(String result) {
Expand All @@ -356,7 +356,7 @@ private void finishSuccess(String result) {
this.setResult(RESULT_SUCCESS, intent);
this.finish();
//关闭窗体动画显示
this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out,0);
this.overridePendingTransition(R.anim.mn_scan_activity_bottom_out, 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.Intent;

import com.google.zxing.client.android.model.MNScanConfig;
import com.google.zxing.client.android.other.ActResultRequest;
import com.google.zxing.client.android.other.MNScanCallback;

/**
* Created by maning on 2017/12/7.
Expand All @@ -12,21 +14,19 @@

public class MNScanManager {

/**
* 默认打开扫描页面
*
* @param context 上下文
* @param requestCode If >= 0, this code will be returned in
* onActivityResult() when the activity exits.
*/
public static void startScan(Activity context, int requestCode) {
Intent intent = new Intent(context.getApplicationContext(), CaptureActivity.class);
context.startActivityForResult(intent, requestCode);
context.overridePendingTransition(R.anim.mn_scan_activity_bottom_in, 0);

public static void startScan(Activity activity, MNScanCallback scanCallback) {
Intent intent = new Intent(activity.getApplicationContext(), CaptureActivity.class);
ActResultRequest actResultRequest = new ActResultRequest(activity);
actResultRequest.startForResult(intent, scanCallback);
activity.overridePendingTransition(R.anim.mn_scan_activity_bottom_in, 0);
}

public static void startScan(Activity context, int requestCode, MNScanConfig mnScanConfig) {
Intent intent = new Intent(context.getApplicationContext(), CaptureActivity.class);
public static void startScan(Activity activity, MNScanConfig mnScanConfig, MNScanCallback scanCallback) {
if (mnScanConfig == null) {
mnScanConfig = new MNScanConfig.Builder().builder();
}
Intent intent = new Intent(activity.getApplicationContext(), CaptureActivity.class);
//是否显示相册按钮
intent.putExtra(CaptureActivity.INTENT_KEY_PHOTO_FLAG, mnScanConfig.isShowPhotoAlbum());
//识别声音
Expand All @@ -37,8 +37,10 @@ public static void startScan(Activity context, int requestCode, MNScanConfig mnS
intent.putExtra(CaptureActivity.INTENT_KEY_SCSNCOLOR, mnScanConfig.getScanColor());
//扫码框上面的提示文案
intent.putExtra(CaptureActivity.INTENT_KEY_HINTTEXT, mnScanConfig.getScanHintText());
context.startActivityForResult(intent, requestCode);
context.overridePendingTransition(mnScanConfig.getActivityOpenAnime(), 0);

ActResultRequest actResultRequest = new ActResultRequest(activity);
actResultRequest.startForResult(intent, scanCallback);
activity.overridePendingTransition(mnScanConfig.getActivityOpenAnime(), 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public final class CameraManager {

private static final int MIN_FRAME_WIDTH = 240;
private static final int MIN_FRAME_HEIGHT = 240;
private static final int MAX_FRAME_WIDTH = 1200; // = 5/8 * 1920
private static final int MAX_FRAME_HEIGHT = 675; // = 5/8 * 1080
private static final int MAX_FRAME_WIDTH = 675;
private static final int MAX_FRAME_HEIGHT = 675;

private final Context context;
private final CameraConfigurationManager configManager;
Expand Down Expand Up @@ -218,7 +218,7 @@ public synchronized Rect getFramingRect() {
}

int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
int height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
int height = width;

int leftOffset = (screenResolution.x - width) / 2;
int topOffset = (screenResolution.y - height) / 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.google.zxing.client.android.other;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;

/**
* <pre>
* author : maning
* e-mail : xxx@xx
* time : 2018/06/04
* desc :
* version: 1.0
* </pre>
*/
public class ActResultRequest {
private OnActResultEventDispatcherFragment fragment;

public ActResultRequest(Activity activity) {
fragment = getEventDispatchFragment(activity);
}

private OnActResultEventDispatcherFragment getEventDispatchFragment(Activity activity) {
final FragmentManager fragmentManager = activity.getFragmentManager();

OnActResultEventDispatcherFragment fragment = findEventDispatchFragment(fragmentManager);
if (fragment == null) {
fragment = new OnActResultEventDispatcherFragment();
fragmentManager
.beginTransaction()
.add(fragment, OnActResultEventDispatcherFragment.TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
return fragment;
}

private OnActResultEventDispatcherFragment findEventDispatchFragment(FragmentManager manager) {
return (OnActResultEventDispatcherFragment) manager.findFragmentByTag(OnActResultEventDispatcherFragment.TAG);
}

public void startForResult(Intent intent, MNScanCallback callback) {
fragment.startForResult(intent, callback);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.google.zxing.client.android.other;

import android.content.Intent;

/**
* <pre>
* author : maning
* e-mail : xxx@xx
* time : 2018/06/04
* desc :
* version: 1.0
* </pre>
*/
public interface MNScanCallback {

void onActivityResult(int resultCode, Intent data);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.google.zxing.client.android.other;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;

/**
* <pre>
* author : maning
* e-mail : xxx@xx
* time : 2018/06/04
* desc :
* version: 1.0
* </pre>
*/
public class OnActResultEventDispatcherFragment extends Fragment {
public static final String TAG = "on_act_result_event_dispatcher";

private SparseArray<MNScanCallback> mCallbacks = new SparseArray<>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

public void startForResult(Intent intent, MNScanCallback callback) {
mCallbacks.put(callback.hashCode(), callback);
startActivityForResult(intent, callback.hashCode());
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

MNScanCallback callback = mCallbacks.get(requestCode);
mCallbacks.remove(requestCode);

if (callback != null) {
callback.onActivityResult(resultCode, data);
}
}
}

0 comments on commit 4a94a3e

Please sign in to comment.