Created
November 25, 2014 10:55
-
-
Save darkwave/43352f775e6f767a11cb to your computer and use it in GitHub Desktop.
QRCode reader using ZXing with Processing under Android (Ketai)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.zxing.*; | |
import java.io.ByteArrayInputStream; | |
import javax.imageio.ImageIO; | |
import com.google.zxing.common.*; | |
import com.google.zxing.client.j2se.*; | |
import android.graphics.Bitmap; | |
import ketai.camera.*; | |
KetaiCamera cam; | |
boolean globalHistogram = false; | |
com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader(); | |
void setup() { | |
orientation(LANDSCAPE); | |
imageMode(CENTER); | |
cam = new KetaiCamera(this, 320, 240, 24); | |
} | |
void draw() { | |
image(cam, width/2, height/2); | |
} | |
void populateYUVLuminanceFromRGB(int[] rgb, byte[] yuv420sp, int width, int height) { | |
for (int i = 0; i < width * height; i++) { | |
float red = (rgb[i] >> 16) & 0xff; | |
float green = (rgb[i] >> 8) & 0xff; | |
float blue = (rgb[i]) & 0xff; | |
int luminance = (int) ((0.257f * red) + (0.504f * green) + (0.098f * blue) + 16); | |
yuv420sp[i] = (byte) (0xff & luminance); | |
} | |
} | |
void onCameraPreviewEvent() | |
{ | |
cam.read(); | |
Bitmap camBitmap = (Bitmap) cam.getNative(); | |
int w = camBitmap.getWidth(); | |
int h = camBitmap.getHeight(); | |
int[] rgb = new int[w * h]; | |
byte[] yuv = new byte[w * h]; | |
camBitmap.getPixels(rgb, 0, w, 0, 0, w, h); | |
populateYUVLuminanceFromRGB(rgb, yuv, w, h); | |
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(yuv, w, h, 0, 0, w, h, false); | |
BinaryBitmap bitmap; | |
if (globalHistogram) | |
bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); | |
else | |
bitmap = new BinaryBitmap(new HybridBinarizer(source)); | |
Result result = null; | |
try { | |
result = reader.decode(bitmap); | |
} | |
catch (Exception e) { | |
} | |
//Once we get the results, we can do some display | |
if (result != null && | |
result.getText() != null) { | |
println(result.getText()); | |
} | |
//LuminanceSource source = new PlanarYUVLuminanceSource(, 0, 0, cam.width, cam.height, false); | |
} | |
// start/stop camera preview by tapping the screen | |
void mousePressed() | |
{ | |
if (cam.isStarted()) | |
{ | |
cam.stop(); | |
} else | |
cam.start(); | |
} | |
void keyPressed() { | |
if (key == CODED) { | |
if (keyCode == MENU) { | |
if (cam.isFlashEnabled()) | |
cam.disableFlash(); | |
else | |
cam.enableFlash(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment