mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 11:47:26 -08:00
Try to support data: URIs
git-svn-id: https://zxing.googlecode.com/svn/trunk@2682 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
04289c004f
commit
13d97af689
|
@ -137,16 +137,7 @@ final class DecodeWorker implements Callable<Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Result decode(URI uri, Map<DecodeHintType,?> hints) throws IOException {
|
private Result decode(URI uri, Map<DecodeHintType,?> hints) throws IOException {
|
||||||
BufferedImage image;
|
BufferedImage image = ImageReader.readImage(uri);
|
||||||
try {
|
|
||||||
image = ImageIO.read(uri.toURL());
|
|
||||||
} catch (IllegalArgumentException ignored) {
|
|
||||||
throw new FileNotFoundException("Resource not found: " + uri);
|
|
||||||
}
|
|
||||||
if (image == null) {
|
|
||||||
System.err.println(uri.toString() + ": Could not load image");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
LuminanceSource source;
|
LuminanceSource source;
|
||||||
if (config.getCrop() == null) {
|
if (config.getCrop() == null) {
|
||||||
|
@ -183,16 +174,7 @@ final class DecodeWorker implements Callable<Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Result[] decodeMulti(URI uri, Map<DecodeHintType,?> hints) throws IOException {
|
private Result[] decodeMulti(URI uri, Map<DecodeHintType,?> hints) throws IOException {
|
||||||
BufferedImage image;
|
BufferedImage image = ImageReader.readImage(uri);
|
||||||
try {
|
|
||||||
image = ImageIO.read(uri.toURL());
|
|
||||||
} catch (IllegalArgumentException ignored) {
|
|
||||||
throw new FileNotFoundException("Resource not found: " + uri);
|
|
||||||
}
|
|
||||||
if (image == null) {
|
|
||||||
System.err.println(uri.toString() + ": Could not load image");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
LuminanceSource source;
|
LuminanceSource source;
|
||||||
if (config.getCrop() == null) {
|
if (config.getCrop() == null) {
|
||||||
|
|
|
@ -87,13 +87,10 @@ public final class GUIRunner extends JFrame {
|
||||||
private static String getDecodeText(File file) {
|
private static String getDecodeText(File file) {
|
||||||
BufferedImage image;
|
BufferedImage image;
|
||||||
try {
|
try {
|
||||||
image = ImageIO.read(file);
|
image = ImageReader.readImage(file);
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
return ioe.toString();
|
return ioe.toString();
|
||||||
}
|
}
|
||||||
if (image == null) {
|
|
||||||
return "Could not decode image";
|
|
||||||
}
|
|
||||||
LuminanceSource source = new BufferedImageLuminanceSource(image);
|
LuminanceSource source = new BufferedImageLuminanceSource(image);
|
||||||
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||||
Result result;
|
Result result;
|
||||||
|
|
75
javase/src/com/google/zxing/client/j2se/ImageReader.java
Normal file
75
javase/src/com/google/zxing/client/j2se/ImageReader.java
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013 ZXing authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.client.j2se;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates reading URIs as images.
|
||||||
|
*
|
||||||
|
* @author Sean Owen
|
||||||
|
*/
|
||||||
|
public final class ImageReader {
|
||||||
|
|
||||||
|
private static final String BASE64TOKEN = "base64,";
|
||||||
|
|
||||||
|
private ImageReader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BufferedImage readImage(URI uri) throws IOException {
|
||||||
|
if ("data".equals(uri.getScheme())) {
|
||||||
|
return readDataURIImage(uri);
|
||||||
|
}
|
||||||
|
BufferedImage result;
|
||||||
|
try {
|
||||||
|
result = ImageIO.read(uri.toURL());
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
throw new IOException("Resource not found: " + uri, iae);
|
||||||
|
}
|
||||||
|
if (result == null) {
|
||||||
|
throw new IOException("Could not load " + uri);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BufferedImage readDataURIImage(URI uri) throws IOException {
|
||||||
|
String uriString = uri.toString();
|
||||||
|
if (!uriString.startsWith("data:image/")) {
|
||||||
|
throw new IOException("Unsupported data URI MIME type");
|
||||||
|
}
|
||||||
|
int base64Start = uriString.indexOf(BASE64TOKEN);
|
||||||
|
if (base64Start < 0) {
|
||||||
|
throw new IOException("Unsupported data URI encoding");
|
||||||
|
}
|
||||||
|
String base64DataEncoded = uriString.substring(base64Start + BASE64TOKEN.length());
|
||||||
|
String base64Data = URLDecoder.decode(base64DataEncoded, "UTF-8");
|
||||||
|
byte[] imageBytes = DatatypeConverter.parseBase64Binary(base64Data);
|
||||||
|
return ImageIO.read(new ByteArrayInputStream(imageBytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BufferedImage readImage(File file) throws IOException {
|
||||||
|
return readImage(file.toURI());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -28,6 +28,7 @@ import com.google.zxing.Reader;
|
||||||
import com.google.zxing.ReaderException;
|
import com.google.zxing.ReaderException;
|
||||||
import com.google.zxing.Result;
|
import com.google.zxing.Result;
|
||||||
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
||||||
|
import com.google.zxing.client.j2se.ImageReader;
|
||||||
import com.google.zxing.common.GlobalHistogramBinarizer;
|
import com.google.zxing.common.GlobalHistogramBinarizer;
|
||||||
import com.google.zxing.common.HybridBinarizer;
|
import com.google.zxing.common.HybridBinarizer;
|
||||||
|
|
||||||
|
@ -126,13 +127,30 @@ public final class DecodeServlet extends HttpServlet {
|
||||||
imageURIString = "http://" + imageURIString;
|
imageURIString = "http://" + imageURIString;
|
||||||
}
|
}
|
||||||
|
|
||||||
URL imageURL;
|
URI imageURI;
|
||||||
try {
|
try {
|
||||||
imageURL = new URI(imageURIString).toURL();
|
imageURI = new URI(imageURIString);
|
||||||
} catch (URISyntaxException ignored) {
|
} catch (URISyntaxException ignored) {
|
||||||
log.info("URI was not valid: " + imageURIString);
|
log.info("URI was not valid: " + imageURIString);
|
||||||
response.sendRedirect("badurl.jspx");
|
response.sendRedirect("badurl.jspx");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shortcut for data URI
|
||||||
|
if ("data".equals(imageURI.getScheme())) {
|
||||||
|
try {
|
||||||
|
BufferedImage image = ImageReader.readDataURIImage(imageURI);
|
||||||
|
processImage(image, request, response);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
log.info(ioe.toString());
|
||||||
|
response.sendRedirect("badurl.jspx");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
URL imageURL;
|
||||||
|
try {
|
||||||
|
imageURL = imageURI.toURL();
|
||||||
} catch (MalformedURLException ignored) {
|
} catch (MalformedURLException ignored) {
|
||||||
log.info("URI was not valid: " + imageURIString);
|
log.info("URI was not valid: " + imageURIString);
|
||||||
response.sendRedirect("badurl.jspx");
|
response.sendRedirect("badurl.jspx");
|
||||||
|
@ -290,6 +308,13 @@ public final class DecodeServlet extends HttpServlet {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
processImage(image, request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void processImage(BufferedImage image,
|
||||||
|
ServletRequest request,
|
||||||
|
HttpServletResponse response) throws IOException, ServletException {
|
||||||
|
|
||||||
Reader reader = new MultiFormatReader();
|
Reader reader = new MultiFormatReader();
|
||||||
LuminanceSource source = new BufferedImageLuminanceSource(image);
|
LuminanceSource source = new BufferedImageLuminanceSource(image);
|
||||||
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
|
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
|
||||||
|
|
Loading…
Reference in a new issue