2012-10-29 96 views
2

我想在图像URL上使用base64编码对图像进行编码。 但它为所有的URL提供相同的编码。使用java的base64图像编码

我的代码如下:

Object namee = url.openConnection().getContentType(); 
String name = (String) namee; 

BufferedImage image = ImageIO.read(url); 

//getting image extension from content type 
String ext = name.substring(name.lastIndexOf("/") + 1); 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

ImageIO.write(image, ext, baos); 
byte[] imageData = baos.toByteArray(); 
String base64value = Base64.encodeBase64URLSafeString(imageData); 
+1

我们能不能看到,从你的代码张贴,无论这些网址或他们的数据在每种情况下实际上是不同的。你应该先检查一下。另外,你使用的是哪个Base64库? – DNA

+0

是的,我正在使用不同的网址..我直接在浏览器中检查它 – Abhinav

+0

是否正确的语法? – Abhinav

回答

0

该代码转换图像文件/数据为Base64格式是什么,但你的形象的文本表示。

要转换此信息,您需要编码器&解码器,您将从http://www.source-code.biz/base64coder/java/获得。这是您将需要的File Base64Coder.java。

我们访问这个类按您的要求,您将需要以下类:

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.InputStream; 
import java.io.IOException; 
import java.io.OutputStream; 

public class Base64 { 

public static void main(String args[]) throws IOException { 
    /* 
    * if (args.length != 2) {System.out.println(
    * "Command line parameters: inputFileName outputFileName"); 
    * System.exit(9); } encodeFile(args[0], args[1]); 
    */ 
    File sourceImage = new File("back3.png"); 
    File sourceImage64 = new File("back3.txt"); 
    File destImage = new File("back4.png"); 
    encodeFile(sourceImage, sourceImage64); 
    decodeFile(sourceImage64, destImage); 
} 

private static void encodeFile(File inputFile, File outputFile) throws IOException { 
    BufferedInputStream in = null; 
    BufferedWriter out = null; 
    try { 
    in = new BufferedInputStream(new FileInputStream(inputFile)); 
    out = new BufferedWriter(new FileWriter(outputFile)); 
    encodeStream(in, out); 
    out.flush(); 
    } finally { 
    if (in != null) 
    in.close(); 
    if (out != null) 
    out.close(); 
    } 
} 

private static void encodeStream(InputStream in, BufferedWriter out) throws IOException { 
    int lineLength = 72; 
    byte[] buf = new byte[lineLength/4 * 3]; 
    while (true) { 
    int len = in.read(buf); 
    if (len <= 0) 
    break; 
    out.write(Base64Coder.encode(buf, 0, len)); 
    out.newLine(); 
    } 
} 

static String encodeArray(byte[] in) throws IOException { 
    StringBuffer out = new StringBuffer(); 
    out.append(Base64Coder.encode(in, 0, in.length)); 
    return out.toString(); 
} 

static byte[] decodeArray(String in) throws IOException { 
    byte[] buf = Base64Coder.decodeLines(in); 
    return buf; 
} 

private static void decodeFile(File inputFile, File outputFile) throws IOException { 
    BufferedReader in = null; 
    BufferedOutputStream out = null; 
    try { 
    in = new BufferedReader(new FileReader(inputFile)); 
    out = new BufferedOutputStream(new FileOutputStream(outputFile)); 
    decodeStream(in, out); 
    out.flush(); 
    } finally { 
    if (in != null) 
    in.close(); 
    if (out != null) 
    out.close(); 
    } 
} 

private static void decodeStream(BufferedReader in, OutputStream out) throws IOException { 
    while (true) { 
    String s = in.readLine(); 
    if (s == null) 
    break; 
    byte[] buf = Base64Coder.decodeLines(s); 
    out.write(buf); 
    } 
} 
} 

在Android中您可以将位图转换为Base64要上传到服务器/ Web服务。

Bitmap bmImage = //Data 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
byte[] imageData = baos.toByteArray(); 
String encodedImage = Base64.encodeArray(imageData); 

这个“encodedImage”是你的图像的文本表示。您可以使用此为任何目的上传或直接进入diplaying HTML页如下:

<img alt="" src="data:image/png;base64,<?php echo $encodedImage; ?>" width="100px" /> 
<img alt="" src="data:image/png;base64,/9j/4AAQ...........1f/9k=" width="100px" /> 
0

这里是工作示例如何convert image to base64 with Java.

public static String encodeToString(BufferedImage image, String type) { 
    String base64String = null; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    try { 
     ImageIO.write(image, type, bos); 
     byte[] imageBytes = bos.toByteArray(); 
     BASE64Encoder encoder = new BASE64Encoder(); 
     base64String = encoder.encode(imageBytes); 
     bos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return base64String; 
}