| 1 | /** |
| 2 | * Copyright Copyright 2014-17 Simon Andrews |
| 3 | * |
| 4 | * This file is part of FastQC. |
| 5 | * |
| 6 | * Conclave is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * SeqMonk is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with Conclave; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | package uk.ac.babraham.FastQC.Utilities; |
| 21 | |
| 22 | import java.awt.image.BufferedImage; |
| 23 | import java.io.ByteArrayOutputStream; |
| 24 | import java.io.IOException; |
| 25 | import java.io.OutputStream; |
| 26 | |
| 27 | import javax.imageio.ImageIO; |
| 28 | |
| 29 | import net.sourceforge.iharder.base64.Base64; |
| 30 | |
| 31 | public class ImageToBase64 { |
| 32 | |
| 33 | public static String imageToBase64 (BufferedImage b) { |
| 34 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 35 | OutputStream b64 = new Base64.OutputStream(os); |
| 36 | |
| 37 | try { |
| 38 | ImageIO.write(b, "PNG", b64); |
| 39 | |
| 40 | return("data:image/png;base64,"+os.toString("UTF-8")); |
| 41 | } |
| 42 | catch (IOException e) { |
| 43 | e.printStackTrace(); |
| 44 | return "Failed"; |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | |
| 49 | public static String svgImageToBase64 (String svgdata) { |
| 50 | |
| 51 | |
| 52 | // We've moved to using the Java.util Base64 encoder which means that |
| 53 | // SVG output will only work on java v8+ but there was a bug in the |
| 54 | // library we were using which caused the last character to get lost |
| 55 | // some times so this is an easy fix and no one is using java <v8 |
| 56 | // any more. |
| 57 | |
| 58 | String data = "data:image/svg+xml;base64,"+java.util.Base64.getEncoder().encodeToString(svgdata.getBytes()); |
| 59 | |
| 60 | return(data); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | } |