mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
Product ported, fails due to required UPCEReader
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 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.result;
|
||||
|
||||
/**
|
||||
* Represents a parsed result that encodes a product by an identifier of some kind.
|
||||
*
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class ProductParsedRXingResult extends ParsedRXingResult {
|
||||
|
||||
private final String productID;
|
||||
private final String normalizedProductID;
|
||||
|
||||
ProductParsedRXingResult(String productID) {
|
||||
this(productID, productID);
|
||||
}
|
||||
|
||||
ProductParsedRXingResult(String productID, String normalizedProductID) {
|
||||
super(ParsedRXingResultType.PRODUCT);
|
||||
this.productID = productID;
|
||||
this.normalizedProductID = normalizedProductID;
|
||||
}
|
||||
|
||||
public String getProductID() {
|
||||
return productID;
|
||||
}
|
||||
|
||||
public String getNormalizedProductID() {
|
||||
return normalizedProductID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayRXingResult() {
|
||||
return productID;
|
||||
}
|
||||
|
||||
}
|
||||
61
src/client/result/ProductParsedResult.rs
Normal file
61
src/client/result/ProductParsedResult.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2007 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.result;
|
||||
|
||||
use super::{ParsedRXingResult, ParsedRXingResultType};
|
||||
|
||||
/**
|
||||
* Represents a parsed result that encodes a product by an identifier of some kind.
|
||||
*
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
pub struct ProductParsedRXingResult {
|
||||
|
||||
product_id:String,
|
||||
normalized_product_id:String,
|
||||
|
||||
}
|
||||
impl ParsedRXingResult for ProductParsedRXingResult {
|
||||
fn getType(&self) -> super::ParsedRXingResultType {
|
||||
ParsedRXingResultType::PRODUCT
|
||||
}
|
||||
|
||||
fn getDisplayRXingResult(&self) -> String {
|
||||
self.product_id.clone()
|
||||
}
|
||||
}
|
||||
impl ProductParsedRXingResult {
|
||||
pub fn new(product_id:String) -> Self {
|
||||
Self::with_normalized_id(product_id.clone(), product_id)
|
||||
}
|
||||
|
||||
pub fn with_normalized_id( product_id: String, normalized_product_id:String) -> Self {
|
||||
Self{
|
||||
product_id,
|
||||
normalized_product_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getProductID(&self) -> &str{
|
||||
&self.product_id
|
||||
}
|
||||
|
||||
pub fn getNormalizedProductID(&self) -> &str {
|
||||
&self.normalized_product_id
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 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.result;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.RXingResult;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link ProductParsedRXingResult}.
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class ProductParsedRXingResultTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testProduct() {
|
||||
doTest("123456789012", "123456789012", BarcodeFormat.UPC_A);
|
||||
doTest("00393157", "00393157", BarcodeFormat.EAN_8);
|
||||
doTest("5051140178499", "5051140178499", BarcodeFormat.EAN_13);
|
||||
doTest("01234565", "012345000065", BarcodeFormat.UPC_E);
|
||||
}
|
||||
|
||||
private static void doTest(String contents, String normalized, BarcodeFormat format) {
|
||||
RXingResult fakeRXingResult = new RXingResult(contents, null, null, format);
|
||||
ParsedRXingResult result = RXingResultParser.parseRXingResult(fakeRXingResult);
|
||||
assertSame(ParsedRXingResultType.PRODUCT, result.getType());
|
||||
ProductParsedRXingResult productRXingResult = (ProductParsedRXingResult) result;
|
||||
assertEquals(contents, productRXingResult.getProductID());
|
||||
assertEquals(normalized, productRXingResult.getNormalizedProductID());
|
||||
}
|
||||
|
||||
}
|
||||
57
src/client/result/ProductParsedResultTestCase.rs
Normal file
57
src/client/result/ProductParsedResultTestCase.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2007 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.result;
|
||||
|
||||
// import com.google.zxing.BarcodeFormat;
|
||||
// import com.google.zxing.RXingResult;
|
||||
// import org.junit.Assert;
|
||||
// import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link ProductParsedRXingResult}.
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
// public final class ProductParsedRXingResultTestCase extends Assert {
|
||||
|
||||
use crate::{BarcodeFormat, RXingResult, client::result::{ParsedRXingResult, ParsedRXingResultType, ProductParsedResult, ParsedClientResult}};
|
||||
|
||||
use super::ResultParser;
|
||||
|
||||
#[test]
|
||||
fn testProduct() {
|
||||
doTest("123456789012", "123456789012", BarcodeFormat::UPC_A);
|
||||
doTest("00393157", "00393157", BarcodeFormat::EAN_8);
|
||||
doTest("5051140178499", "5051140178499", BarcodeFormat::EAN_13);
|
||||
doTest("01234565", "012345000065", BarcodeFormat::UPC_E);
|
||||
}
|
||||
|
||||
fn doTest( contents:&str, normalized:&str, format:BarcodeFormat) {
|
||||
let fakeRXingResult = RXingResult::new(contents, Vec::new(), Vec::new(), format);
|
||||
let result = ResultParser::parseRXingResult(&fakeRXingResult);
|
||||
assert_eq!(ParsedRXingResultType::PRODUCT, result.getType());
|
||||
|
||||
if let ParsedClientResult::ProductResult(productRXingResult) = result {
|
||||
assert_eq!(contents, productRXingResult.getProductID());
|
||||
assert_eq!(normalized, productRXingResult.getNormalizedProductID());
|
||||
}else{
|
||||
panic!("Expected ParsedClientResult::ProductResult")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// }
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 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.result;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.RXingResult;
|
||||
import com.google.zxing.oned.UPCEReader;
|
||||
|
||||
/**
|
||||
* Parses strings of digits that represent a UPC code.
|
||||
*
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class ProductRXingResultParser extends RXingResultParser {
|
||||
|
||||
// Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes.
|
||||
@Override
|
||||
public ProductParsedRXingResult parse(RXingResult result) {
|
||||
BarcodeFormat format = result.getBarcodeFormat();
|
||||
if (!(format == BarcodeFormat.UPC_A || format == BarcodeFormat.UPC_E ||
|
||||
format == BarcodeFormat.EAN_8 || format == BarcodeFormat.EAN_13)) {
|
||||
return null;
|
||||
}
|
||||
String rawText = getMassagedText(result);
|
||||
if (!isStringOfDigits(rawText, rawText.length())) {
|
||||
return null;
|
||||
}
|
||||
// Not actually checking the checksum again here
|
||||
|
||||
String normalizedProductID;
|
||||
// Expand UPC-E for purposes of searching
|
||||
if (format == BarcodeFormat.UPC_E && rawText.length() == 8) {
|
||||
normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText);
|
||||
} else {
|
||||
normalizedProductID = rawText;
|
||||
}
|
||||
|
||||
return new ProductParsedRXingResult(rawText, normalizedProductID);
|
||||
}
|
||||
|
||||
}
|
||||
57
src/client/result/ProductResultParser.rs
Normal file
57
src/client/result/ProductResultParser.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2007 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.result;
|
||||
|
||||
// import com.google.zxing.BarcodeFormat;
|
||||
// import com.google.zxing.RXingResult;
|
||||
// import com.google.zxing.oned.UPCEReader;
|
||||
|
||||
use crate::{RXingResult, BarcodeFormat};
|
||||
|
||||
use super::{ParsedClientResult, ProductParsedRXingResult, ResultParser};
|
||||
|
||||
/**
|
||||
* Parses strings of digits that represent a UPC code.
|
||||
*
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
|
||||
// Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes.
|
||||
|
||||
let format = result.getBarcodeFormat();
|
||||
if !(format == &BarcodeFormat::UPC_A || format == &BarcodeFormat::UPC_E ||
|
||||
format == &BarcodeFormat::EAN_8 || format == &BarcodeFormat::EAN_13) {
|
||||
return None;
|
||||
}
|
||||
let rawText = ResultParser::getMassagedText(result);
|
||||
if !ResultParser::isStringOfDigits(&rawText, rawText.len()) {
|
||||
return None;
|
||||
}
|
||||
// Not actually checking the checksum again here
|
||||
|
||||
let normalizedProductID;
|
||||
// Expand UPC-E for purposes of searching
|
||||
if format == &BarcodeFormat::UPC_E && rawText.len() == 8 {
|
||||
unimplemented!("UPCEReader is required to parse this");
|
||||
//normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText);
|
||||
} else {
|
||||
normalizedProductID = rawText.clone();
|
||||
}
|
||||
|
||||
Some(ParsedClientResult::ProductResult(ProductParsedRXingResult::with_normalized_id(rawText, normalizedProductID)))
|
||||
|
||||
}
|
||||
@@ -35,7 +35,7 @@ use crate::{exceptions::Exceptions, RXingResult};
|
||||
|
||||
use super::{
|
||||
GeoResultParser, ISBNResultParser, ParsedClientResult, ParsedRXingResult, SMSMMSResultParser,
|
||||
TelResultParser, TextParsedRXingResult, WifiResultParser,
|
||||
TelResultParser, TextParsedRXingResult, WifiResultParser, ProductResultParser,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -102,7 +102,7 @@ pub fn getMassagedText(result: &RXingResult) -> String {
|
||||
}
|
||||
|
||||
pub fn parseRXingResult(theRXingResult: &RXingResult) -> ParsedClientResult {
|
||||
let PARSERS: [&ParserFunction; 5] = [
|
||||
let PARSERS: [&ParserFunction; 6] = [
|
||||
// new BookmarkDoCoMoRXingResultParser(),
|
||||
// new AddressBookDoCoMoRXingResultParser(),
|
||||
// new EmailDoCoMoRXingResultParser(),
|
||||
@@ -120,7 +120,7 @@ pub fn parseRXingResult(theRXingResult: &RXingResult) -> ParsedClientResult {
|
||||
// new URLTORXingResultParser(),
|
||||
// new URIRXingResultParser(),
|
||||
&ISBNResultParser::parse,
|
||||
// new ProductRXingResultParser(),
|
||||
&ProductResultParser::parse,
|
||||
// new ExpandedProductRXingResultParser(),
|
||||
// new VINRXingResultParser(),
|
||||
];
|
||||
|
||||
@@ -12,6 +12,8 @@ mod GeoResultParser;
|
||||
mod GeoParsedResult;
|
||||
mod SMSParsedResult;
|
||||
mod SMSMMSResultParser;
|
||||
mod ProductParsedResult;
|
||||
mod ProductResultParser;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
@@ -28,6 +30,7 @@ pub use WifiParsedResult::*;
|
||||
pub use GeoParsedResult::*;
|
||||
// pub use GeoResultParser::*;
|
||||
pub use SMSParsedResult::*;
|
||||
pub use ProductParsedResult::*;
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -40,6 +43,8 @@ mod WifiParsedResultTestCase;
|
||||
mod GeoParsedResultTestCase;
|
||||
#[cfg(test)]
|
||||
mod SMSMMSParsedResultTestCase;
|
||||
#[cfg(test)]
|
||||
mod ProductParsedResultTestCase;
|
||||
|
||||
pub enum ParsedClientResult {
|
||||
TextResult(TextParsedRXingResult),
|
||||
@@ -48,6 +53,7 @@ pub enum ParsedClientResult {
|
||||
WiFiResult(WifiParsedRXingResult),
|
||||
GeoResult(GeoParsedRXingResult),
|
||||
SMSResult(SMSParsedRXingResult),
|
||||
ProductResult(ProductParsedRXingResult),
|
||||
}
|
||||
|
||||
impl ParsedRXingResult for ParsedClientResult {
|
||||
@@ -59,6 +65,7 @@ impl ParsedRXingResult for ParsedClientResult {
|
||||
ParsedClientResult::WiFiResult(a) => a.getType(),
|
||||
ParsedClientResult::GeoResult(a) => a.getType(),
|
||||
ParsedClientResult::SMSResult(a) => a.getType(),
|
||||
ParsedClientResult::ProductResult(a) => a.getType(),
|
||||
|
||||
|
||||
}
|
||||
@@ -72,6 +79,7 @@ impl ParsedRXingResult for ParsedClientResult {
|
||||
ParsedClientResult::WiFiResult(a) => a.getDisplayRXingResult(),
|
||||
ParsedClientResult::GeoResult(a) => a.getDisplayRXingResult(),
|
||||
ParsedClientResult::SMSResult(a) => a.getDisplayRXingResult(),
|
||||
ParsedClientResult::ProductResult(a) => a.getDisplayRXingResult(),
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user