All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.org.okapibarcode.gui.SaveImage Maven / Gradle / Ivy

Go to download

An open-source barcode generator written entirely in Java, supporting over 50 encoding standards including all ISO standards.

There is a newer version: 0.4.9
Show newest version
/*
 * Copyright 2014 Robin Stuart and Robert Elliott
 *
 * 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 uk.org.okapibarcode.gui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import uk.org.okapibarcode.output.PostScriptRenderer;
import uk.org.okapibarcode.output.SvgRenderer;

/**
 * Save bar code image to image file
 *
 * @author Robert Elliott
 */
public class SaveImage {

    public void saveImage(File file, JPanel panel) throws IOException {
        int magnification = 1;
        int borderSize = 5;

        String extension = "";
        int i = file.getName().lastIndexOf('.');
        if (i > 0) {
            extension = file.getName().substring(i + 1);
        }

        switch (extension) {
            case "png":
            case "gif":
            case "jpg":
            case "bmp":
                BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
                panel.paint(img.getGraphics());
                ImageIO.write(img, extension, file);
                break;
            case "svg":
                SvgRenderer svg = new SvgRenderer(new FileOutputStream(file), magnification, borderSize, OkapiUI.paperColour, OkapiUI.inkColour);
                svg.render(OkapiUI.symbol);
                break;
            case "eps":
                PostScriptRenderer eps = new PostScriptRenderer(new FileOutputStream(file), magnification, borderSize, OkapiUI.paperColour, OkapiUI.inkColour);
                eps.render(OkapiUI.symbol);
                break;
            default:
                System.out.println("Unsupported output format");
                break;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy