在这篇文章中,我们将讨论如何使用 Java 创建条形码的细节,以及使用相同的 Java API 识别条形码。我们已经熟悉条形码是一种编码标签或标签,贴在所有商品上,允许计算机更快速、更准确地跟踪和结算产品。同样,它们可以很容易地打印并及时添加到物品上,以便您的整个库存立即被标记和跟踪。此外,条形码提供了自动产品识别、极快的识别速度以及在非常低的错误率下实施数据。这种方法具有极高的性价比,可以帮助雇主降低间接成本,减少培训时间和劳动力。

条形码生成API

Aspose.BarCode Cloud 是一个 REST API,使我们的客户能够在编程中实现条形码生成和处理功能。

该 API 能够解析流行的光栅图像格式。有关更多详细信息,请访问 Supported File Formats

生成具有指定尺寸的条形码

测试我们 API 的快速且简单的方法是使用 API 参考部分。GetBarcodeGenerate 方法用于生成条形码图像,并提供了广泛的选项,以根据您的要求生成条形码,以及与特定参数相关的详细信息在 API 参考页面上有很好的解释。在生成条形码时,您可以指定以下任一类型

  • Codabar
  • Code11
  • Code39Standard
  • Code39Extended
  • Code93Standard
  • Code93Extended
  • Code128
  • GS1Code128
  • EAN8
  • EAN13
  • EAN14
  • SCC14
  • SSCC18
  • UPCA
  • UPCE
  • ISBN
  • ISSN
  • ISMN
  • Standard2of5
  • Interleaved2of5
  • Matrix2of5
  • ItalianPost25
  • IATA2of5
  • ITF14
  • ITF6
  • MSI
  • VIN
  • DeutschePostIdentcode
  • DeutschePostLeitcode
  • OPC
  • PZN
  • Code16K
  • Pharmacode
  • DataMatrix
  • QR
  • 阿兹特克
  • Pdf417
  • MacroPdf417
  • AustraliaPost
  • Postnet
  • 行星
  • OneCode
  • RM4SCC
  • DatabarOmniDirectional
  • DatabarTruncated
  • DatabarLimited
  • DatabarExpanded
  • SingaporePost
  • GS1DataMatrix
  • AustralianPosteParcel
  • SwissPostParcel
  • PatchCode
  • DatabarExpandedStacked
  • DatabarStacked
  • DatabarStackedOmniDirectional
  • MicroPdf417
  • GS1QR
  • MaxiCode
  • Code32
  • DataLogic2of5
  • DotCode
  • DutchKIX
  • UpcaGs1Code128Coupon
  • UpcaGs1DatabarCoupon
  • CodablockF
  • GS1CodablockF

cURL 命令

curl -X GET "https://api.aspose.cloud/v3.0/barcode/generate?Type=Codabar&Text=Hello%20World&TwoDDisplayText=QR&TextLocation=Above&TextAlignment=Center&TextColor=Blue&FontSizeMode=Auto&Resolution=96&TextSpace=2&Units=Point&SizeMode=Nearest&BarHeight=50&ImageHeight=200&ImageWidth=200&RotationAngle=0&BackColor=White&BorderDashStyle=Solid&SupplementData=EAN8&format=PNG" -H  "accept: image/png" -H  "authorization: Bearer <JWT Token>"

请求 URL

https://api.aspose.cloud/v3.0/barcode/generate?Type=Codabar&Text=Hello%20World&TwoDDisplayText=QR&TextLocation=Above&TextAlignment=Center&TextColor=Blue&FontSizeMode=Auto&Resolution=96&TextSpace=2&Units=Point&SizeMode=Nearest&BarHeight=50&ImageHeight=200&ImageWidth=200&RotationAngle=0&BackColor=White&BorderDashStyle=Solid&SupplementData=EAN8&format=PNG

Java

以下代码片段可用于生成条形码图像并将其存储在云存储中。

// 有关完整的示例和数据文件,请访问 https://github.com/aspose-barcode-cloud/aspose-barcode-cloud-java/

String name = "putBarcodeGenerateFile.png";
String type = EncodeBarcodeType.QR.getValue();
String text = "Put Barcode Generate File";
Double dimensionX = 200.0;

String twoDDisplayText = null;
String textLocation = null;
String textAlignment = null;
String textColor = null;
String fontSizeMode = null;
Double resolution = null;
Double textSpace = null;
String units = null;
String sizeMode = null;
Double barHeight = null;
Double imageHeight = null;
Double imageWidth = null;
Double rotationAngle = null;
String backColor = null;
String barColor = null;
String borderColor = null;
Double borderWidth = null;
String borderDashStyle = null;
Boolean borderVisible = null;
String enableChecksum = null;
Boolean enableEscape = null;
Boolean filledBars = null;
Boolean alwaysShowChecksum = null;
Double wideNarrowRatio = null;
Boolean validateText = null;
String supplementData = null;
Double supplementSpace = null;
String format = null;

String storage = "Storage";
String folder = "Temp/"

ResultImageInfo response =
		api.putBarcodeGenerateFile(
				name,type,text,twoDDisplayText,textLocation,textAlignment,textColor,fontSizeMode,
				resolution,null,null,dimensionX,textSpace,units,sizeMode,barHeight,
				imageHeight,imageWidth,rotationAngle,backColor,barColor,borderColor,borderWidth,
				borderDashStyle,borderVisible,enableChecksum,enableEscape,filledBars,
				alwaysShowChecksum,wideNarrowRatio,validateText,supplementData,
				supplementSpace,storage,folder,format);

assertTrue(response.getFileSize() > 0);
assertTrue(response.getImageWidth() > 0);
assertTrue(response.getImageHeight() > 0);
条形码图像

Image 1:- 使用 Java API 生成的条形码预览

识别光栅图像中的条形码

API 还提供了从光栅图像文件中识别条形码的功能。图像文件可以是 PNG、JPEG、BMP、GIF、TIFF 或 EXIF 格式。API 的 GetBarcodeRecognize 方法使您能够实现这一需求。名称字段是必填的,其他可选字段提供了更好的识别操作的能力,因为您可以指定条形码类型信息、校验和验证、编码、允许配置识别质量和速度的预设等等。

cURL

curl -X GET "https://api.aspose.cloud/v3.0/barcode/index.png/recognize?Type=all&ChecksumValidation=Off&DetectEncoding=false&Preset=HighQuality" -H  "accept: application/json" -H  "authorization: Bearer <JWT Token>"

请求 URL

https://api.aspose.cloud/v3.0/barcode/index.png/recognize?Type=all&ChecksumValidation=Off&DetectEncoding=false&Preset=HighQuality

从外部图像 URL 读取条形码

// 对于完整的示例和数据文件,请访问 https://github.com/aspose-barcode-cloud/aspose-barcode-cloud-java/

String type = null;
String checksumValidation = ChecksumValidation.OFF.toString();
Boolean detectEncoding = null;
String preset = PresetType.HIGHPERFORMANCE.toString();
String url = "http://www.barcoding.com/images/Barcodes/pdf417.gif";

Integer rectX = null;
Integer rectY = null;
Integer rectWidth = null;
Integer rectHeight = null;
Boolean stripFNC = null;
Integer timeout = null;
Integer medianSmoothingWindowSize = null;
Boolean allowMedianSmoothing = null;
Boolean allowComplexBackground = null;
Boolean allowDatamatrixIndustrialBarcodes = null;
Boolean allowDecreasedImage = null;
Boolean allowDetectScanGap = null;
Boolean allowIncorrectBarcodes = null;
Boolean allowInvertImage = null;
Boolean allowMicroWhiteSpotsRemoving = null;
Boolean allowOneDFastBarcodesDetector = null;
Boolean allowOneDWipedBarsRestoration = null;
Boolean allowQRMicroQrRestoration = null;
Boolean allowRegularImage = null;
Boolean allowSaltAndPepperFiltering = null;
Boolean allowWhiteSpotsRemoving = null;
Double regionLikelihoodThresholdPercent = null;
List<Integer> scanWindowSizes = null;
Double similarity = null;
Boolean skipDiagonalSearch = null;
String australianPostEncodingTable = null;
String rectangleRegion = null;
File image = null;

BarcodeResponseList response = 
   	 api.postBarcodeRecognizeFromUrlOrContent( 
      		type,checksumValidation,detectEncoding,preset,rectX,rectY,rectWidth,rectHeight,
      		stripFNC,timeout,medianSmoothingWindowSize,allowMedianSmoothing,allowComplexBackground,
      		allowDatamatrixIndustrialBarcodes,allowDecreasedImage,allowDetectScanGap,
		allowIncorrectBarcodes,allowInvertImage,allowMicroWhiteSpotsRemoving,allowOneDFastBarcodesDetector,
      		allowOneDWipedBarsRestoration,allowQRMicroQrRestoration,allowRegularImage,allowSaltAndPepperFiltering,
      		allowWhiteSpotsRemoving,regionLikelihoodThresholdPercent,scanWindowSizes,similarity,skipDiagonalSearch,
      		australianPostEncodingTable,rectangleRegion,url,image);

assertNotNull(response);
assertFalse(response.getBarcodes().isEmpty());

BarcodeResponse barcode = response.getBarcodes().get(0);
assertEquals(DecodeBarcodeType.CODE11.getValue(), barcode.getType());
assertEquals("1234567812", barcode.getBarcodeValue());

结论

在本文中,我们讨论了如何使用 Java 以编程方式创建条形码的步骤和相关细节,以及如何使用 Java 代码片段读取和识别现有条形码。您可能注意到,所有这些操作都是通过几行简单的代码完成的。无需额外的软件下载或安装,这些操作可以用更少的代码行完成。

相关文章

我们建议您访问以下链接以了解更多信息关于