[LIB-9] Add ability to archive qrcode content
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / zip / ZipChessQRCodeWriter.java
diff --git a/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/zip/ZipChessQRCodeWriter.java b/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/zip/ZipChessQRCodeWriter.java
new file mode 100644 (file)
index 0000000..6b92e76
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * 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 org.hedgecode.chess.qrcode.zip;
+
+import java.util.Map;
+
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.common.BitMatrix;
+import org.hedgecode.chess.img.ImageFormat;
+import org.hedgecode.chess.qrcode.ChessQRCodeException;
+import org.hedgecode.chess.qrcode.ChessQRCodeMode;
+import org.hedgecode.chess.qrcode.ChessQRCodeWriter;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class ZipChessQRCodeWriter extends ChessQRCodeWriter {
+
+    private static ChessQRCodeWriter _instance = new ZipChessQRCodeWriter();
+
+    protected ZipChessQRCodeWriter() {
+        super();
+    }
+
+    @Override
+    protected BitMatrix encode(
+            ChessQRCodeMode mode,
+            String contents,
+            int width, int height,
+            Map<EncodeHintType,?> hints)
+            throws ChessQRCodeException
+    {
+        if (mode == null || mode.isArchive()) {
+            throw new ChessQRCodeException(
+                    ChessQRCodeException.Type.WRITE, "write.qrcode.mode.null"
+            );
+        }
+
+        if (contents == null || contents.isEmpty()) {
+            throw new ChessQRCodeException(
+                    ChessQRCodeException.Type.WRITE, "write.qrcode.contents.empty"
+            );
+        }
+
+        String zipContents = ZipContentDeflater.deflate(
+                mode.name().concat(contents)
+        );
+
+        return super.encode(
+                ChessQRCodeMode.ZIP, zipContents, width, height, hints
+        );
+    }
+
+    public static ChessQRCodeWriter getInstance() {
+        return _instance;
+    }
+
+
+    public static void main(String[] args) {
+        try {
+            ZipChessQRCodeWriter.getInstance().write(
+                    ChessQRCodeMode.PGN,
+                    "1.e4 c6 2.d4 d5 3.Nc3 dxe4 4.Nxe4 Nd7 5.Ng5 Ngf6 6.Bd3 e6 7.N1f3 h6\n" +
+                            "8.Nxe6 Qe7 9.O-O fxe6 10.Bg6+ Kd8 {Каспаров встряхнул головой} \n" +
+                            "11.Bf4 b5 12.a4 Bb7 13.Re1 Nd5 14.Bg3 Kc8 15.axb5 cxb5 16.Qd3 Bc6 \n" +
+                            "17.Bf5 exf5 18.Rxe7 Bxe7 19.c4 1-0",
+                    ImageFormat.PNG,
+                    "./qrcode.png"
+            );
+        } catch (ChessQRCodeException e) {
+            System.out.println("Could not generate QR Code, Exception: " + e.getMessage());
+        }
+    }
+
+}