mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 03:37:34 -08:00
Fix incorrect byte display in some cases, refactor and simplify and even speed up output a little
git-svn-id: https://zxing.googlecode.com/svn/trunk@2648 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
58bfbcd212
commit
c835ed3be6
66
zxingorg/src/com/google/zxing/web/OutputUtils.java
Normal file
66
zxingorg/src/com/google/zxing/web/OutputUtils.java
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* 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.web;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Utility functions for {@code decoderesult.jspx}.
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class OutputUtils {
|
||||
|
||||
private static final int BYTES_PER_LINE = 16;
|
||||
private static final int HALF_BYTES_PER_LINE = BYTES_PER_LINE / 2;
|
||||
|
||||
private static final Pattern NEWLINE = Pattern.compile("\r?\n");
|
||||
|
||||
private OutputUtils() {
|
||||
}
|
||||
|
||||
public static String arrayToString(byte[] bytes) {
|
||||
StringBuilder result = new StringBuilder(bytes.length * 4);
|
||||
int i = 0;
|
||||
while (i < bytes.length) {
|
||||
int value = bytes[i] & 0xFF;
|
||||
result.append(hexChar(value / 16));
|
||||
result.append(hexChar(value % 16));
|
||||
i++;
|
||||
if (i % BYTES_PER_LINE == 0) {
|
||||
result.append('\n');
|
||||
} else if (i % HALF_BYTES_PER_LINE == 0) {
|
||||
result.append(" ");
|
||||
} else {
|
||||
result.append(' ');
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static char hexChar(int value) {
|
||||
if (value < 0 || value > 15) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return (char) (value < 10 ? ('0' + value) : ('a' + (value - 10)));
|
||||
}
|
||||
|
||||
public static String escapeNewlineAsBR(CharSequence in) {
|
||||
return NEWLINE.matcher(in).replaceAll("<br/>");
|
||||
}
|
||||
|
||||
}
|
|
@ -20,36 +20,18 @@
|
|||
com.google.zxing.Result,
|
||||
com.google.zxing.client.result.ResultParser,
|
||||
com.google.zxing.client.result.ParsedResult,
|
||||
com.google.zxing.web.OutputUtils,
|
||||
org.apache.commons.lang3.StringEscapeUtils"/>
|
||||
<jsp:declaration>
|
||||
private static String arrayToString(byte[] bytes) {
|
||||
int length = bytes.length;
|
||||
StringBuilder result = new StringBuilder(length << 2);
|
||||
int i = 0;
|
||||
while (i < length) {
|
||||
int max = Math.min(i + 8, length);
|
||||
for (int j = i; j < max; j++) {
|
||||
int value = bytes[j] & 0xFF;
|
||||
result.append(Integer.toHexString(value / 16));
|
||||
result.append(Integer.toHexString(value % 16));
|
||||
result.append(' ');
|
||||
}
|
||||
result.append('\n');
|
||||
i += 8;
|
||||
}
|
||||
for (int j = i - 8; j < length; j++) {
|
||||
result.append(Integer.toHexString(bytes[j] & 0xFF));
|
||||
result.append(' ');
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
</jsp:declaration>
|
||||
<jsp:directive.page contentType="text/html" session="false"/>
|
||||
<jsp:scriptlet>response.setHeader("Cache-Control", "no-cache");</jsp:scriptlet>
|
||||
<jsp:scriptlet>
|
||||
response.setHeader("Cache-Control", "no-cache");
|
||||
response.setHeader("X-Robots-Tag", "none");
|
||||
</jsp:scriptlet>
|
||||
<jsp:text><![CDATA[<!DOCTYPE html>]]></jsp:text>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="robots" content="none"/>
|
||||
<title>Decode Succeeded</title>
|
||||
<link rel="stylesheet" href="style.css" type="text/css"/>
|
||||
<style type="text/css">
|
||||
|
@ -67,8 +49,7 @@
|
|||
if (text == null) {
|
||||
text = "(Not applicable)";
|
||||
} else {
|
||||
text = StringEscapeUtils.escapeXml(text);
|
||||
text = text.replaceAll("\r?\n", "<br/>");
|
||||
text = OutputUtils.escapeNewlineAsBR(StringEscapeUtils.escapeXml(text));
|
||||
}
|
||||
|
||||
byte[] rawBytes = result.getRawBytes();
|
||||
|
@ -76,25 +57,24 @@
|
|||
if (rawBytes == null) {
|
||||
rawBytesString = "(Not applicable)";
|
||||
} else {
|
||||
rawBytesString = arrayToString(rawBytes);
|
||||
rawBytesString = OutputUtils.arrayToString(rawBytes);
|
||||
}
|
||||
|
||||
String displayResult = parsedResult.getDisplayResult();
|
||||
if (displayResult == null) {
|
||||
displayResult = "(Not applicable)";
|
||||
} else {
|
||||
displayResult = StringEscapeUtils.escapeXml(displayResult);
|
||||
displayResult = displayResult.replaceAll("\r?\n", "<br/>");
|
||||
displayResult = OutputUtils.escapeNewlineAsBR(StringEscapeUtils.escapeXml(displayResult));
|
||||
}
|
||||
</jsp:scriptlet>
|
||||
<table>
|
||||
<table style="width:100%">
|
||||
<tr>
|
||||
<td>Raw text</td>
|
||||
<td><jsp:expression>text</jsp:expression></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Raw bytes</td>
|
||||
<td><jsp:expression>rawBytesString</jsp:expression></td>
|
||||
<td><pre style="margin:0"><jsp:expression>rawBytesString</jsp:expression></pre></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Barcode format</td>
|
||||
|
|
Loading…
Reference in a new issue