move decoding to class

This commit is contained in:
ligi 2014-06-04 21:31:02 +02:00
parent 1344ade633
commit 60bce93518
2 changed files with 48 additions and 37 deletions

View file

@ -1,24 +1,14 @@
package org.ligi.passandroid;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.squareup.spoon.Spoon;
import org.ligi.passandroid.helper.BarcodeDecoder;
import org.ligi.passandroid.injections.FixedPassBook;
import org.ligi.passandroid.injections.FixedPassListPassStore;
import org.ligi.passandroid.model.Passbook;
@ -41,29 +31,6 @@ public class TheFullscreenBarcodeActivity extends ActivityInstrumentationTestCas
super(FullscreenBarcodeActivity.class);
}
public static String readQRImage(Bitmap bMap) {
String contents = null;
int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();// use this otherwise ChecksumException
try {
Result result = reader.decode(bitmap);
contents = result.getText();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return contents;
}
@Override
public void setUp() throws Exception {
@ -87,11 +54,12 @@ public class TheFullscreenBarcodeActivity extends ActivityInstrumentationTestCas
App.getPassStore().setCurrentPass(pass);
getActivity();
onView(withId(R.id.fullscreen_image)).check(matches(isDisplayed()));
Spoon.screenshot(getActivity(), "pdf417_barcode");
final ImageView viewById = ButterKnife.findById(getActivity(), R.id.fullscreen_image);
BitmapDrawable bitmapDrawable = (BitmapDrawable) viewById.getDrawable();
assertThat(readQRImage(bitmapDrawable.getBitmap())).isEqualTo("foo");
assertThat(BarcodeDecoder.decodeBitmap(bitmapDrawable.getBitmap())).isEqualTo("foo");
}
@ -102,11 +70,12 @@ public class TheFullscreenBarcodeActivity extends ActivityInstrumentationTestCas
App.getPassStore().setCurrentPass(pass);
getActivity();
onView(withId(R.id.fullscreen_image)).check(matches(isDisplayed()));
Spoon.screenshot(getActivity(), "aztec_barcode");
final ImageView viewById = ButterKnife.findById(getActivity(), R.id.fullscreen_image);
BitmapDrawable bitmapDrawable = (BitmapDrawable) viewById.getDrawable();
assertThat(readQRImage(bitmapDrawable.getBitmap())).isEqualTo("foo");
assertThat(BarcodeDecoder.decodeBitmap(bitmapDrawable.getBitmap())).isEqualTo("foo");
}
@ -117,11 +86,12 @@ public class TheFullscreenBarcodeActivity extends ActivityInstrumentationTestCas
App.getPassStore().setCurrentPass(pass);
getActivity();
onView(withId(R.id.fullscreen_image)).check(matches(isDisplayed()));
Spoon.screenshot(getActivity(), "qr_barcode");
final ImageView viewById = ButterKnife.findById(getActivity(), R.id.fullscreen_image);
BitmapDrawable bitmapDrawable = (BitmapDrawable) viewById.getDrawable();
assertThat(readQRImage(bitmapDrawable.getBitmap())).isEqualTo("foo");
assertThat(BarcodeDecoder.decodeBitmap(bitmapDrawable.getBitmap())).isEqualTo("foo");
}
}

View file

@ -0,0 +1,41 @@
package org.ligi.passandroid.helper;
import android.graphics.Bitmap;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
public class BarcodeDecoder {
public static String decodeBitmap(Bitmap bMap) {
String contents = null;
int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();// use this otherwise ChecksumException
try {
Result result = reader.decode(bitmap);
contents = result.getText();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
return contents;
}
}