Here you can find the source of imageToBase64String(RenderedImage image, String type)
Parameter | Description |
---|---|
image | The image to encode |
type | jpeg, bmp, ... |
public static String imageToBase64String(RenderedImage image, String type)
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Open Door Logistics (www.opendoorlogistics.com) * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at http://www.gnu.org/licenses/lgpl.txt ******************************************************************************/ import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import sun.misc.BASE64Encoder; public class Main { /**/* w w w.j av a 2 s.c o m*/ * Encode image to string * * @param image * The image to encode * @param type * jpeg, bmp, ... * @return encoded string */ public static String imageToBase64String(RenderedImage image, String type) { String ret = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, type, bos); byte[] bytes = bos.toByteArray(); BASE64Encoder encoder = new BASE64Encoder(); ret = encoder.encode(bytes); ret = ret.replace(System.lineSeparator(), ""); } catch (IOException e) { throw new RuntimeException(); } return ret; } private static byte[] toByteArray(BufferedImage img, String imageFileType) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(img, imageFileType, baos); return baos.toByteArray(); } catch (Throwable e) { throw new RuntimeException(); } } }