Skip to content

Commit

Permalink
api zxing core v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
zz4760762 committed Nov 14, 2019
1 parent b761097 commit 2af64f9
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 197 deletions.
13 changes: 8 additions & 5 deletions zxinglib/src/main/java/pony/xcode/zxing/AmbientLightManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class AmbientLightManager implements SensorEventListener {

private final Context context;
private CameraManager cameraManager;
private SensorManager sensorManager;
private Sensor lightSensor;

AmbientLightManager(Context context) {
Expand All @@ -52,19 +53,21 @@ void start(CameraManager cameraManager) {
this.cameraManager = cameraManager;
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (FrontLightMode.readPref(sharedPrefs) == FrontLightMode.AUTO) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor != null) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
}

void stop() {
if (lightSensor != null) {
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(this);
if (sensorManager != null) {
sensorManager.unregisterListener(this);
}
cameraManager = null;
sensorManager = null;
lightSensor = null;
}
}
Expand Down
21 changes: 5 additions & 16 deletions zxinglib/src/main/java/pony/xcode/zxing/BeepManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,21 @@ synchronized void playBeepSoundAndVibrate() {
// }

private MediaPlayer buildMediaPlayer() {
MediaPlayer mediaPlayer = MediaPlayer.create(activity, R.raw.zxl_beep);
MediaPlayer mediaPlayer = null;
try {
mediaPlayer = MediaPlayer.create(activity, R.raw.zxl_beep);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(false);
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
// mediaPlayer.prepare();
return mediaPlayer;
} catch (Exception e) {
Log.w(TAG, e);
mediaPlayer.release();
if (mediaPlayer != null) {
mediaPlayer.release();
}
return null;
}
// try (AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.zxl_beep)) {
// mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
// mediaPlayer.setOnErrorListener(this);
// mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// mediaPlayer.setLooping(false);
// mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
// mediaPlayer.prepare();
// return mediaPlayer;
// } catch (IOException ioe) {
// Log.w(TAG, ioe);
// mediaPlayer.release();
// return null;
// }
}

@Override
Expand Down
43 changes: 23 additions & 20 deletions zxinglib/src/main/java/pony/xcode/zxing/CaptureFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -36,15 +37,12 @@ public class CaptureFragment extends Fragment implements OnCaptureCallback {

private View mRootView;

private SurfaceView surfaceView;
private ViewfinderView viewfinderView;

private CaptureHelper mCaptureHelper;

public static CaptureFragment newInstance() {

Bundle args = new Bundle();

CaptureFragment fragment = new CaptureFragment();
fragment.setArguments(args);
return fragment;
Expand All @@ -54,8 +52,8 @@ public static CaptureFragment newInstance() {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int layoutId = getLayoutId();
if(isContentView(layoutId)){
mRootView = inflater.inflate(getLayoutId(),container,false);
if (isContentView(layoutId)) {
mRootView = inflater.inflate(getLayoutId(), container, false);
}
initUI();
return mRootView;
Expand All @@ -64,60 +62,64 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
/**
* 初始化
*/
public void initUI(){
surfaceView = mRootView.findViewById(getSurfaceViewId());
viewfinderView = mRootView.findViewById(getViewfinderViewId());
mCaptureHelper = new CaptureHelper(this,surfaceView,viewfinderView);
public void initUI() {
SurfaceView surfaceView = mRootView.findViewById(getSurfaceViewId());
ViewfinderView viewfinderView = mRootView.findViewById(getViewfinderViewId());
mCaptureHelper = new CaptureHelper(this, surfaceView, viewfinderView);
mCaptureHelper.setOnCaptureCallback(this);
}

/**
* 返回true时会自动初始化{@link #mRootView},返回为false时需自己去通过{@link #setRootView(View)}初始化{@link #mRootView}
* @param layoutId
*
* @return 默认返回true
*/
public boolean isContentView(@LayoutRes int layoutId){
public boolean isContentView(@LayoutRes int layoutId) {
return true;
}

/**
* 布局id
*
* @return
*/
public int getLayoutId(){
@LayoutRes
public int getLayoutId() {
return R.layout.zxl_capture;
}

/**
* {@link ViewfinderView} 的 id
* @return
*/
public int getViewfinderViewId(){
@IdRes
public int getViewfinderViewId() {
return R.id.viewfinderView;
}


/**
* 预览界面{@link #surfaceView} 的id
* @return
* 预览界面{@link SurfaceView} 的id
*/
public int getSurfaceViewId(){
@IdRes
public int getSurfaceViewId() {
return R.id.surfaceView;
}

/**
* Get {@link CaptureHelper}
*
* @return {@link #mCaptureHelper}
*/
public CaptureHelper getCaptureHelper(){
public CaptureHelper getCaptureHelper() {
return mCaptureHelper;
}

/**
* Get {@link CameraManager}
*
* @return {@link #mCaptureHelper#getCameraManager()}
*/
public CameraManager getCameraManager(){
public CameraManager getCameraManager() {
return mCaptureHelper.getCameraManager();
}

Expand Down Expand Up @@ -159,6 +161,7 @@ public void onDestroy() {

/**
* 接收扫码结果回调
*
* @param result 扫码结果
* @return 返回true表示拦截,将不自动执行后续逻辑,为false表示不拦截,默认不拦截
*/
Expand Down
72 changes: 38 additions & 34 deletions zxinglib/src/main/java/pony/xcode/zxing/DecodeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.view.Display;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
Expand Down Expand Up @@ -55,7 +57,7 @@ final class DecodeHandler extends Handler {

private long lastZoomTime;

DecodeHandler(Context context, CameraManager cameraManager,CaptureHandler handler, Map<DecodeHintType,Object> hints) {
DecodeHandler(Context context, CameraManager cameraManager, CaptureHandler handler, Map<DecodeHintType, Object> hints) {
multiFormatReader = new MultiFormatReader();
multiFormatReader.setHints(hints);
this.context = context;
Expand All @@ -64,21 +66,23 @@ final class DecodeHandler extends Handler {
}

@Override
public void handleMessage(Message message) {
if (message == null || !running) {
public void handleMessage(@NonNull Message message) {
if (!running) {
return;
}
if (message.what == R.id.decode) {
decode((byte[]) message.obj, message.arg1, message.arg2,isScreenPortrait(),handler.isSupportVerticalCode());
decode((byte[]) message.obj, message.arg1, message.arg2, isScreenPortrait(), handler.isSupportVerticalCode());

} else if (message.what == R.id.quit) {
running = false;
Looper.myLooper().quit();

Looper looper = Looper.myLooper();
if (looper != null) {
looper.quit();
}
}
}

private boolean isScreenPortrait(){
private boolean isScreenPortrait() {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point screenResolution = new Point();
Expand All @@ -94,27 +98,27 @@ private boolean isScreenPortrait(){
* @param width The width of the preview frame.
* @param height The height of the preview frame.
*/
private void decode(byte[] data, int width, int height,boolean isScreenPortrait,boolean isSupportVerticalCode) {
private void decode(byte[] data, int width, int height, boolean isScreenPortrait, boolean isSupportVerticalCode) {
long start = System.currentTimeMillis();
Result rawResult = null;
PlanarYUVLuminanceSource source = buildPlanarYUVLuminanceSource(data,width,height,isScreenPortrait);
PlanarYUVLuminanceSource source = buildPlanarYUVLuminanceSource(data, width, height, isScreenPortrait);

if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = multiFormatReader.decodeWithState(bitmap);
} catch (Exception e) {
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try{
try {
rawResult = multiFormatReader.decodeWithState(bitmap1);
}catch (Exception e1){
if(isSupportVerticalCode){
source = buildPlanarYUVLuminanceSource(data,width,height,!isScreenPortrait);
if(source!=null){
} catch (Exception e1) {
if (isSupportVerticalCode) {
source = buildPlanarYUVLuminanceSource(data, width, height, !isScreenPortrait);
if (source != null) {
BinaryBitmap bitmap2 = new BinaryBitmap(new HybridBinarizer(source));
try{
try {
rawResult = multiFormatReader.decodeWithState(bitmap2);
}catch (Exception e2){
} catch (Exception ignored) {

}
}
Expand All @@ -133,24 +137,24 @@ private void decode(byte[] data, int width, int height,boolean isScreenPortrait,
Log.d(TAG, "Found barcode in " + (end - start) + " ms");

BarcodeFormat barcodeFormat = rawResult.getBarcodeFormat();
if(handler!=null && handler.isSupportAutoZoom() && barcodeFormat == BarcodeFormat.QR_CODE){
if (handler != null && handler.isSupportAutoZoom() && barcodeFormat == BarcodeFormat.QR_CODE) {

ResultPoint[] resultPoints = rawResult.getResultPoints();
if(resultPoints.length >= 3){
float distance1 = ResultPoint.distance(resultPoints[0],resultPoints[1]);
float distance2 = ResultPoint.distance(resultPoints[1],resultPoints[2]);
float distance3 = ResultPoint.distance(resultPoints[0],resultPoints[2]);
int maxDistance = (int)Math.max(Math.max(distance1,distance2),distance3);
if(handleAutoZoom(maxDistance,width)){
if (resultPoints.length >= 3) {
float distance1 = ResultPoint.distance(resultPoints[0], resultPoints[1]);
float distance2 = ResultPoint.distance(resultPoints[1], resultPoints[2]);
float distance3 = ResultPoint.distance(resultPoints[0], resultPoints[2]);
int maxDistance = (int) Math.max(Math.max(distance1, distance2), distance3);
if (handleAutoZoom(maxDistance, width)) {
Message message = Message.obtain();
message.what = R.id.decode_succeeded;
message.obj = rawResult;
if(handler.isReturnBitmap()){
if (handler.isReturnBitmap()) {
Bundle bundle = new Bundle();
bundleThumbnail(source, bundle);
message.setData(bundle);
}
handler.sendMessageDelayed(message,300);
handler.sendMessageDelayed(message, 300);
return;
}
}
Expand All @@ -160,7 +164,7 @@ private void decode(byte[] data, int width, int height,boolean isScreenPortrait,

if (handler != null) {
Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
if(handler.isReturnBitmap()){
if (handler.isReturnBitmap()) {
Bundle bundle = new Bundle();
bundleThumbnail(source, bundle);
message.setData(bundle);
Expand All @@ -175,9 +179,9 @@ private void decode(byte[] data, int width, int height,boolean isScreenPortrait,
}
}

private PlanarYUVLuminanceSource buildPlanarYUVLuminanceSource(byte[] data, int width, int height,boolean isRotate){
private PlanarYUVLuminanceSource buildPlanarYUVLuminanceSource(byte[] data, int width, int height, boolean isRotate) {
PlanarYUVLuminanceSource source;
if(isRotate){
if (isRotate) {
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
Expand All @@ -187,7 +191,7 @@ private PlanarYUVLuminanceSource buildPlanarYUVLuminanceSource(byte[] data, int
width = height;
height = tmp;
source = cameraManager.buildLuminanceSource(rotatedData, width, height);
}else{
} else {
source = cameraManager.buildLuminanceSource(data, width, height);
}
return source;
Expand All @@ -204,20 +208,20 @@ private static void bundleThumbnail(PlanarYUVLuminanceSource source, Bundle bund
bundle.putFloat(DecodeThread.BARCODE_SCALED_FACTOR, (float) width / source.getWidth());
}

private boolean handleAutoZoom(int length,int width){
if(lastZoomTime > System.currentTimeMillis() - 1000){
private boolean handleAutoZoom(int length, int width) {
if (lastZoomTime > System.currentTimeMillis() - 1000) {
return true;
}

if(length<width/5){
if (length < width / 5) {

Camera camera = cameraManager.getOpenCamera().getCamera();
if(camera!=null){
if (camera != null) {
Camera.Parameters params = camera.getParameters();
if (params.isZoomSupported()) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
params.setZoom(Math.min(zoom + maxZoom/5,maxZoom));
params.setZoom(Math.min(zoom + maxZoom / 5, maxZoom));
camera.setParameters(params);
lastZoomTime = System.currentTimeMillis();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static class InactivityAsyncTask extends AsyncTask<Object,Object,Object>

private WeakReference<Activity> weakReference;

public InactivityAsyncTask(Activity activity){
InactivityAsyncTask(Activity activity){
weakReference = new WeakReference<>(activity);
}

Expand Down
1 change: 0 additions & 1 deletion zxinglib/src/main/java/pony/xcode/zxing/Intents.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public static final class Scan {

/**
* If a barcode is found, Barcodes returns {@link android.app.Activity#RESULT_OK} to
* {@link android.app.Activity#onActivityResult(int, int, Intent)}
* of the app which requested the scan via
* {@link android.app.Activity#startActivityForResult(Intent, int)}
* The barcodes contents can be retrieved with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static class AutoFocusTask extends AsyncTask<Object,Object,Object> {

private WeakReference<AutoFocusManager> weakReference;

public AutoFocusTask(AutoFocusManager manager){
AutoFocusTask(AutoFocusManager manager){
weakReference = new WeakReference<>(manager);
}

Expand Down
Loading

0 comments on commit 2af64f9

Please sign in to comment.