|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * SciJava polyglot kernel for Jupyter. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2017 Hadrien Mary |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package org.scijava.notebook.converter; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Base64; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.Logger; |
| 26 | +import org.knowm.xchart.BitmapEncoder; |
| 27 | +import org.knowm.xchart.internal.chartpart.Chart; |
| 28 | +import org.scijava.Priority; |
| 29 | +import org.scijava.convert.Converter; |
| 30 | +import org.scijava.notebook.converter.output.PNGImageNotebookOutput; |
| 31 | +import org.scijava.plugin.Plugin; |
| 32 | + |
| 33 | +@Plugin(type = Converter.class, priority = Priority.LOW_PRIORITY) |
| 34 | +public class ChartToPNGNotebookConverter |
| 35 | + extends NotebookOutputConverter<Chart, PNGImageNotebookOutput> { |
| 36 | + |
| 37 | + @Override |
| 38 | + public Class<Chart> getInputType() { |
| 39 | + return Chart.class; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Class<PNGImageNotebookOutput> getOutputType() { |
| 44 | + return PNGImageNotebookOutput.class; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public PNGImageNotebookOutput convert(Object object) { |
| 49 | + |
| 50 | + Chart chart = (Chart) object; |
| 51 | + |
| 52 | + byte[] bt; |
| 53 | + try { |
| 54 | + bt = BitmapEncoder.getBitmapBytes(chart, BitmapEncoder.BitmapFormat.PNG); |
| 55 | + Base64.getEncoder().encodeToString(bt); |
| 56 | + return new PNGImageNotebookOutput(Base64.getEncoder().encodeToString(bt)); |
| 57 | + } catch (IOException ex) { |
| 58 | + Logger.getLogger(ChartToPNGNotebookConverter.class.getName()).log(Level.SEVERE, null, ex); |
| 59 | + |
| 60 | + } |
| 61 | + return null; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments