[LIB-9] Add ability to archive qrcode content
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / zip / ZipChessQRCodeReader.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.qrcode.zip;
18
19 import java.io.File;
20
21 import com.google.zxing.common.DecoderResult;
22
23 import org.hedgecode.chess.qrcode.ChessQRCodeException;
24 import org.hedgecode.chess.qrcode.ChessQRCodeMode;
25 import org.hedgecode.chess.qrcode.ChessQRCodeReader;
26 import org.hedgecode.chess.qrcode.ChessQRResult;
27
28 /**
29  *
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public class ZipChessQRCodeReader extends ChessQRCodeReader {
34
35     private static ZipChessQRCodeReader _instance = new ZipChessQRCodeReader();
36
37     protected ZipChessQRCodeReader() {
38         super();
39     }
40
41     @Override
42     protected ChessQRResult createResult(DecoderResult decoderResult) throws ChessQRCodeException {
43         String result = decoderResult.getText();
44         ChessQRCodeMode mode = ChessQRResult.getMode(result);
45
46         if (ChessQRCodeMode.ZIP.equals(mode)) {
47             String contents = ZipContentInflater.inflate(
48                     ChessQRResult.getContents(result)
49             );
50
51             mode = ChessQRResult.getMode(contents);
52             if (mode == null) {
53                 throw new ChessQRCodeException(
54                         ChessQRCodeException.Type.READ, "read.unknown.chess.mode"
55                 );
56             }
57
58             return new ChessQRResult(
59                     mode,
60                     ChessQRResult.getContents(contents)
61             );
62         } else {
63             return super.createResult(decoderResult);
64         }
65     }
66
67     public static ZipChessQRCodeReader getInstance() {
68         return _instance;
69     }
70
71
72     public static void main(String[] args) {
73         try {
74             ChessQRResult qrCodeResult = ZipChessQRCodeReader.getInstance().read(
75                     new File("qrcode.png")
76             );
77             System.out.println("Decoded format = " + qrCodeResult.getMode());
78             System.out.println("Decoded text = " + qrCodeResult.getContents());
79         } catch (ChessQRCodeException e) {
80             System.out.println("Could not decode QR Code. Exception: " + e.getMessage());
81         }
82     }
83
84 }