[LIB-9] Modify ChessQRCodeWriterTest
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / zip / ZipChessQRCodeWriter.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.util.Map;
20
21 import com.google.zxing.EncodeHintType;
22 import com.google.zxing.common.BitMatrix;
23 import org.hedgecode.chess.img.ImageFormat;
24 import org.hedgecode.chess.qrcode.ChessQRCodeException;
25 import org.hedgecode.chess.qrcode.ChessQRCodeMode;
26 import org.hedgecode.chess.qrcode.ChessQRCodeWriter;
27
28 /**
29  *
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public class ZipChessQRCodeWriter extends ChessQRCodeWriter {
34
35     private static ChessQRCodeWriter _instance = new ZipChessQRCodeWriter();
36
37     protected ZipChessQRCodeWriter() {
38         super();
39     }
40
41     @Override
42     protected BitMatrix encode(
43             ChessQRCodeMode mode,
44             String contents,
45             int width, int height,
46             Map<EncodeHintType,?> hints)
47             throws ChessQRCodeException
48     {
49         if (mode == null || mode.isArchive()) {
50             throw new ChessQRCodeException(
51                     ChessQRCodeException.Type.WRITE, "write.qrcode.mode.null"
52             );
53         }
54
55         if (contents == null || contents.isEmpty()) {
56             throw new ChessQRCodeException(
57                     ChessQRCodeException.Type.WRITE, "write.qrcode.contents.empty"
58             );
59         }
60
61         String zipContents = ZipContentDeflater.deflate(
62                 mode.name().concat(contents)
63         );
64
65         return super.encode(
66                 ChessQRCodeMode.ZIP, zipContents, width, height, hints
67         );
68     }
69
70     public static ChessQRCodeWriter getInstance() {
71         return _instance;
72     }
73
74
75     public static void main(String[] args) {
76         try {
77             ZipChessQRCodeWriter.getInstance().write(
78                     ChessQRCodeMode.PGN,
79                     "1.e4 c6 2.d4 d5 3.Nc3 dxe4 4.Nxe4 Nd7 5.Ng5 Ngf6 6.Bd3 e6 7.N1f3 h6\n" +
80                             "8.Nxe6 Qe7 9.O-O fxe6 10.Bg6+ Kd8 {Каспаров встряхнул головой} \n" +
81                             "11.Bf4 b5 12.a4 Bb7 13.Re1 Nd5 14.Bg3 Kc8 15.axb5 cxb5 16.Qd3 Bc6 \n" +
82                             "17.Bf5 exf5 18.Rxe7 Bxe7 19.c4 1-0",
83                     ImageFormat.PNG,
84                     "./qrcode.png"
85             );
86         } catch (ChessQRCodeException e) {
87             System.out.println("Could not generate QR Code, Exception: " + e.getMessage());
88         }
89     }
90
91 }