Posted by
dscho on
URL: http://imagej.273.s1.nabble.com/Announcing-QR-Decoder-for-ImageJ-tp3683501p3683502.html
Hi Elliott,
On Sun, 31 Jul 2011, Elliott Slaughter wrote:
Nice. I made the following change so that the attached image can be
decoded. In total, I made these changes:
- support all image input types
- abort in case of failure, rather than showing a failed result
- as a consequence, I do not need the GenericDialog anymore.
-- snipsnap --
diff -r 9d4c0ddb4272 src/QR_Decoder.java
--- a/src/QR_Decoder.java Sun Jul 31 12:08:11 2011 -0700
+++ b/src/QR_Decoder.java Fri Aug 12 03:32:16 2011 -0700
@@ -1,6 +1,8 @@
import ij.ImagePlus;
+import ij.Macro;
-import ij.gui.GenericDialog;
import ij.plugin.filter.PlugInFilter;
+import ij.process.ByteProcessor;
import ij.process.ImageProcessor;
import ij.text.TextWindow;
@@ -43,32 +45,27 @@
public class QR_Decoder implements PlugInFilter {
public int setup(String arg, ImagePlus image) {
- return DOES_8G | NO_CHANGES;
+ return DOES_ALL | NO_CHANGES;
}
public void run(ImageProcessor ip) {
- GenericDialog gd = new GenericDialog("QR Decoder Settings");
- gd.addStringField("Error Results In: ", "FAILED");
- gd.showDialog();
- if (gd.wasCanceled()) {
- return;
- }
- String defaultResultText = gd.getNextString();
-
+ if (!(ip instanceof ByteProcessor))
+ ip = ip.convertToByte(true);
+ else if (!ip.isDefaultLut())
+ ip = ip.convertToRGB().convertToByte(true);
Reader reader = new QRCodeReader();
BufferedImage myimg = ip.getBufferedImage();
LuminanceSource source = new BufferedImageLuminanceSource(myimg);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
- String resultText = defaultResultText;
+ String resultText = "FAILED";
try {
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = reader.decode(bitmap, hints);
resultText = result.getText();
// On errors just return the default result.
- } catch (NotFoundException e) {
- } catch (ChecksumException e) {
- } catch (FormatException e) {
+ } catch (Exception e) {
+ Macro.abort();
}
new TextWindow("QR Code", resultText, 12 * resultText.length(), 150);