mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Use JCommander for CommandLineEncoder args
This commit is contained in:
parent
8fe986a9aa
commit
23893a0d4b
|
@ -16,7 +16,7 @@
|
|||
|
||||
package com.google.zxing.client.j2se;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.beust.jcommander.JCommander;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
|
@ -29,80 +29,27 @@ import java.util.Locale;
|
|||
* @author Sean Owen
|
||||
*/
|
||||
public final class CommandLineEncoder {
|
||||
|
||||
private static final BarcodeFormat DEFAULT_BARCODE_FORMAT = BarcodeFormat.QR_CODE;
|
||||
private static final String DEFAULT_IMAGE_FORMAT = "PNG";
|
||||
private static final String DEFAULT_OUTPUT_FILE = "out";
|
||||
private static final int DEFAULT_WIDTH = 300;
|
||||
private static final int DEFAULT_HEIGHT = 300;
|
||||
|
||||
private CommandLineEncoder() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 0) {
|
||||
printUsage();
|
||||
EncoderConfig config = new EncoderConfig();
|
||||
JCommander jCommander = new JCommander(config, args);
|
||||
jCommander.setProgramName(CommandLineEncoder.class.getSimpleName());
|
||||
if (config.help) {
|
||||
jCommander.usage();
|
||||
return;
|
||||
}
|
||||
|
||||
BarcodeFormat barcodeFormat = DEFAULT_BARCODE_FORMAT;
|
||||
String imageFormat = DEFAULT_IMAGE_FORMAT;
|
||||
String outFileString = DEFAULT_OUTPUT_FILE;
|
||||
int width = DEFAULT_WIDTH;
|
||||
int height = DEFAULT_HEIGHT;
|
||||
String contents = null;
|
||||
|
||||
for (String arg : args) {
|
||||
String[] argValue = arg.split("=");
|
||||
switch (argValue[0]) {
|
||||
case "--barcode_format":
|
||||
barcodeFormat = BarcodeFormat.valueOf(argValue[1]);
|
||||
break;
|
||||
case "--image_format":
|
||||
imageFormat = argValue[1];
|
||||
break;
|
||||
case "--output":
|
||||
outFileString = argValue[1];
|
||||
break;
|
||||
case "--width":
|
||||
width = Integer.parseInt(argValue[1]);
|
||||
break;
|
||||
case "--height":
|
||||
height = Integer.parseInt(argValue[1]);
|
||||
break;
|
||||
default:
|
||||
if (arg.startsWith("-")) {
|
||||
System.err.println("Unknown command line option " + arg);
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
contents = arg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (contents == null) {
|
||||
printUsage();
|
||||
return;
|
||||
String outFileString = config.outputFileBase;
|
||||
if (EncoderConfig.DEFAULT_OUTPUT_FILE_BASE.equals(outFileString)) {
|
||||
outFileString += '.' + config.imageFormat.toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
|
||||
if (DEFAULT_OUTPUT_FILE.equals(outFileString)) {
|
||||
outFileString += '.' + imageFormat.toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
|
||||
BitMatrix matrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height);
|
||||
MatrixToImageWriter.writeToPath(matrix, imageFormat, Paths.get(outFileString));
|
||||
}
|
||||
|
||||
private static void printUsage() {
|
||||
System.err.println("Encodes barcode images using the ZXing library\n");
|
||||
System.err.println("usage: CommandLineEncoder [ options ] content_to_encode");
|
||||
System.err.println(" --barcode_format=format: Format to encode, from BarcodeFormat class. " +
|
||||
"Not all formats are supported. Defaults to QR_CODE.");
|
||||
System.err.println(" --image_format=format: image output format, such as PNG, JPG, GIF. Defaults to PNG");
|
||||
System.err.println(" --output=filename: File to write to. Defaults to out.png");
|
||||
System.err.println(" --width=pixels: Image width. Defaults to 300");
|
||||
System.err.println(" --height=pixels: Image height. Defaults to 300");
|
||||
BitMatrix matrix = new MultiFormatWriter().encode(
|
||||
config.contents.get(0), config.barcodeFormat, config.width, config.height);
|
||||
MatrixToImageWriter.writeToPath(matrix, config.imageFormat, Paths.get(outFileString));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright 2015 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 java.util.List;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.validators.PositiveInteger;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
final class EncoderConfig {
|
||||
|
||||
static final String DEFAULT_OUTPUT_FILE_BASE = "out";
|
||||
|
||||
@Parameter(names = "--barcode_format",
|
||||
description = "Format to encode, from BarcodeFormat class. Not all formats are supported")
|
||||
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
|
||||
|
||||
@Parameter(names = "--image_format",
|
||||
description = "image output format, such as PNG, JPG, GIF")
|
||||
String imageFormat = "PNG";
|
||||
|
||||
@Parameter(names = "--output",
|
||||
description = " File to write to. Defaults to out.png")
|
||||
String outputFileBase = DEFAULT_OUTPUT_FILE_BASE;
|
||||
|
||||
@Parameter(names = "--width",
|
||||
description = "Image width",
|
||||
validateWith = PositiveInteger.class)
|
||||
int width = 300;
|
||||
|
||||
@Parameter(names = "--height",
|
||||
description = "Image height",
|
||||
validateWith = PositiveInteger.class)
|
||||
int height = 300;
|
||||
|
||||
@Parameter(names = "--help",
|
||||
description = "Prints this help message",
|
||||
help = true)
|
||||
boolean help;
|
||||
|
||||
@Parameter(description = "(Text to encode)", required = true)
|
||||
List<String> contents;
|
||||
|
||||
}
|
Loading…
Reference in a new issue