Skip to content

Commit

Permalink
Remove duplicate results when it has structured append header. (#1147)
Browse files Browse the repository at this point in the history
* remove duplicate results in processStructuredAppend

* add whitespace

* remove illegal import
  • Loading branch information
makiuchi-d authored and srowen committed Mar 22, 2019
1 parent 8a53ade commit fedfa7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Expand Up @@ -96,7 +96,7 @@ public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints)
}
}

private static List<Result> processStructuredAppend(List<Result> results) {
static List<Result> processStructuredAppend(List<Result> results) {
boolean hasSA = false;

// first, check, if there is at least on SA result in the list
Expand All @@ -114,9 +114,10 @@ private static List<Result> processStructuredAppend(List<Result> results) {
List<Result> newResults = new ArrayList<>();
List<Result> saResults = new ArrayList<>();
for (Result result : results) {
newResults.add(result);
if (result.getResultMetadata().containsKey(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE)) {
saResults.add(result);
} else {
newResults.add(result);
}
}
// sort and concatenate the SA list items
Expand Down
Expand Up @@ -19,8 +19,10 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
Expand All @@ -29,6 +31,7 @@
import com.google.zxing.LuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.MultipleBarcodeReader;
Expand Down Expand Up @@ -70,4 +73,38 @@ public void testMultiQRCodes() throws Exception {
assertEquals(expectedContents, barcodeContents);
}

@Test
public void testProcessStructuredAppend() {
Result sa1 = new Result("SA1", new byte[]{}, new ResultPoint[]{}, BarcodeFormat.QR_CODE);
Result sa2 = new Result("SA2", new byte[]{}, new ResultPoint[]{}, BarcodeFormat.QR_CODE);
Result sa3 = new Result("SA3", new byte[]{}, new ResultPoint[]{}, BarcodeFormat.QR_CODE);
sa1.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, (0 << 4) + 2);
sa1.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L");
sa2.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, (1 << 4) + 2);
sa2.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L");
sa3.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, (2 << 4) + 2);
sa3.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L");

Result nsa = new Result("NotSA", new byte[]{}, new ResultPoint[]{}, BarcodeFormat.QR_CODE);
nsa.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, "L");

List<Result> inputs = new ArrayList<>();
inputs.add(sa3);
inputs.add(sa1);
inputs.add(nsa);
inputs.add(sa2);

List<Result> results = QRCodeMultiReader.processStructuredAppend(inputs);
assertNotNull(results);
assertEquals(2, results.size());

Collection<String> barcodeContents = new HashSet<>();
for (Result result : results) {
barcodeContents.add(result.getText());
}
Collection<String> expectedContents = new HashSet<>();
expectedContents.add("SA1SA2SA3");
expectedContents.add("NotSA");
assertEquals(expectedContents, barcodeContents);
}
}

0 comments on commit fedfa7a

Please sign in to comment.