Created
March 24, 2023 16:17
-
-
Save ceaksan/ae197aa3782396b715e2155b574890b5 to your computer and use it in GitHub Desktop.
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 processing.video.*; | |
import com.google.zxing.*; | |
import com.google.zxing.common.*; | |
import com.google.zxing.qrcode.*; | |
Capture cam; //Set up the camera | |
Reader reader = new QRCodeReader(); | |
PImage cover; //This will have the cover image | |
String lastISBNAcquired = ""; //This is the last ISBN we acquired | |
int WIDTH = 640; | |
int HEIGHT = 480; | |
void setup() { | |
size(WIDTH, HEIGHT); | |
cam = new Capture(this, WIDTH, HEIGHT); | |
cam.start(); | |
} | |
void draw() { | |
if (cam.available()) { | |
cam.read(); | |
image(cam, 0, 0); | |
try { | |
// Now test to see if it has a QR code embedded in it | |
LuminanceSource source = new BufferedImageLuminanceSource((BufferedImage) cam.getNative()); | |
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); | |
Result result = reader.decode(bitmap); | |
//Once we get the results, we can do some display | |
if (result.getText() != null) { | |
println(result.getText()); | |
ResultPoint[] points = result.getResultPoints(); | |
//Draw some ellipses on at the control points | |
for (int i = 0; i < points.length; i++) { | |
fill(#ff8c00); | |
ellipse(points[i].getX(), points[i].getY(), 20, 20); | |
} | |
//Now fetch the book cover, if it is found | |
if (!result.getText().equals(lastISBNAcquired)) { | |
String url = result.getText(); | |
try { | |
cover = loadImage(url, "gif"); | |
lastISBNAcquired = result.getText(); | |
} catch (Exception e) { | |
println("Error loading image: " + e.getMessage()); | |
} | |
} | |
//Superimpose the cover on the image | |
if (cover != null) { | |
image(cover, points[1].getX(), points[1].getY()); | |
} | |
} | |
} catch (NotFoundException e) { | |
//QR code not found, do nothing | |
} catch (Exception e) { | |
println("Error decoding QR code: " + e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment