Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-budiyev committed Aug 5, 2018
1 parent 0b831f6 commit e9693b8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import android.support.annotation.Nullable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
Expand All @@ -47,7 +46,6 @@
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

/**
* Utils for decoding and encoding bar codes
Expand Down Expand Up @@ -121,8 +119,7 @@ public static Result decodeRgb(@NonNull final int[] pixels, final int width, fin
Objects.requireNonNull(pixels);
final MultiFormatReader reader = createReader(hints);
try {
return reader.decodeWithState(
new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(width, height, pixels))));
return Utils.decodeLuminanceSource(reader, new RGBLuminanceSource(width, height, pixels));
} catch (final ReaderException e) {
return null;
}
Expand Down Expand Up @@ -171,9 +168,9 @@ public static Result decodeYuv(@NonNull final byte[] pixels, final int width, fi
}
final MultiFormatReader reader = createReader(hints);
try {
return reader.decodeWithState(new BinaryBitmap(new HybridBinarizer(
return Utils.decodeLuminanceSource(reader,
new PlanarYUVLuminanceSource(rotatedPixels, rotatedWidth, rotatedHeight, 0, 0, rotatedWidth,
rotatedHeight, reverseHorizontal))));
rotatedHeight, reverseHorizontal));
} catch (final ReaderException e) {
return null;
}
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/com/budiyev/android/codescanner/DecodeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

final class DecodeTask {
private final byte[] mImage;
Expand Down Expand Up @@ -74,18 +71,8 @@ public Result decode(@NonNull final MultiFormatReader reader) throws ReaderExcep
if (frameWidth < 1 || frameHeight < 1) {
return null;
}
final LuminanceSource luminanceSource =
return Utils.decodeLuminanceSource(reader,
new PlanarYUVLuminanceSource(image, imageWidth, imageHeight, frameRect.getLeft(), frameRect.getTop(),
frameWidth, frameHeight, mReverseHorizontal);
try {
final Result result = reader.decodeWithState(new BinaryBitmap(new HybridBinarizer(luminanceSource)));
if (result != null) {
return result;
} else {
return reader.decodeWithState(new BinaryBitmap(new HybridBinarizer(luminanceSource.invert())));
}
} finally {
reader.reset();
}
frameWidth, frameHeight, mReverseHorizontal));
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/budiyev/android/codescanner/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@
import android.content.Context;
import android.hardware.Camera;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Surface;
import android.view.WindowManager;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

final class Utils {
private static final float MIN_DISTORTION = 0.3f;
private static final float MAX_DISTORTION = 3f;
Expand Down Expand Up @@ -284,6 +292,21 @@ public static byte[] rotateYuv(@NonNull final byte[] source, final int width, fi
return output;
}

@Nullable
public static Result decodeLuminanceSource(@NonNull final MultiFormatReader reader,
@NonNull final LuminanceSource luminanceSource) throws ReaderException {
try {
final Result result = reader.decodeWithState(new BinaryBitmap(new HybridBinarizer(luminanceSource)));
if (result != null) {
return result;
} else {
return reader.decodeWithState(new BinaryBitmap(new HybridBinarizer(luminanceSource.invert())));
}
} finally {
reader.reset();
}
}

public static final class SuppressErrorCallback implements ErrorCallback {
@Override
public void onError(@NonNull final Exception error) {
Expand Down

0 comments on commit e9693b8

Please sign in to comment.