import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TestImageBinary {
static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
public static void main(String[] args) {
// System.out.println(getImageBinary());
//
// base64StringToImage(getImageBinary());
new TestImageBinary().new Image2Binary().mainl(null);
}
static String getImageBinary(){
File f = new File("c://test.jpg");
BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
//test
byte2hex(bytes);
String strImg = encoder.encodeBuffer(bytes).trim();
return strImg;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static void base64StringToImage(String base64String){
try {
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(bais);
File w2 = new File("c://QQ.bmp");//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 方法二,方法二和方法一的区别都是用ImageIO,不同的是方法一用的是加密和解密,而方法二用的是字节转成16进制的char[]表现形式,最后new String 成字符串
*/
/**
* @Title getImgeHexString
* @Description 网络图片转换成二进制字符串
* @param URLName 网络图片地址
* @param type 图片类型
* @return String 转换结果
* @throws
*/
public static String getImgeHexString(String URLName,String type) {
String res = null;
try {
int HttpResult = 0; // 服务器返回的状态
URL url = new URL(URLName); // 创建URL
URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
HttpResult = httpconn.getResponseCode();
System.out.println(HttpResult);
if (HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK则连接不成功
System.out.print("fail");
else {
BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());
BufferedImage bm = ImageIO.read(bis);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bm, type, bos);
bos.flush();
byte[] data = bos.toByteArray();
res = byte2hex(data);
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
/**
* @title 根据二进制字符串生成图片
* @param data 生成图片的二进制字符串
* @param fileName 图片名称(完整路径)
* @param type 图片类型
* @return
*/
public static void saveImage(String data, String fileName,String type) {
BufferedImage image = new BufferedImage(300, 300,BufferedImage.TYPE_BYTE_BINARY);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, byteOutputStream);
// byte[] date = byteOutputStream.toByteArray();
byte[] bytes = hex2byte(data);
System.out.println("path:" + fileName);
RandomAccessFile file = new RandomAccessFile(fileName, "rw");
file.write(bytes);
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 反格式化byte
*
* @param s
* @return
*/
public static byte[] hex2byte(String s) {
byte[] src = s.toLowerCase().getBytes();
byte[] ret = new byte[src.length / 2];
for (int i = 0; i < src.length; i += 2) {
byte hi = src[i];
byte low = src[i + 1];
hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a')
: hi - '0');
low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a')
: low - '0');
ret[i / 2] = (byte) (hi << 4 | low);
}
return ret;
}
/**
* 格式化byte
*
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] out = new char[b.length * 2];
for (int i = 0; i < b.length; i++) {
byte c = b[i];
out[i * 2] = Digit[(c >>> 4) & 0X0F];
out[i * 2 + 1] = Digit[c & 0X0F];
System.out.println(out[i*2]+"-->"+out[i*2+1]);
}
return new String(out);
}
/**
* 方法三
* @author Administrator
*
*/
class Image2Binary {
public void mainl(String[] args)
{
String path = "c://test.jpg";
File file = new File(path);
FileInputStream fis;
try
{
fis = new FileInputStream(file);
byte[] b;
b = new byte[fis.available()];
StringBuilder str = new StringBuilder();// 不建议用String
fis.read(b);
for (byte bs : b) {
//add fujiarui test
System.out.println(bs);
System.out.println(Integer.toBinaryString(bs));
// add fujiarui
str.append(Integer.toBinaryString(bs));// 转换为二进制
}
System.out.println(str);
File apple = new File("c://test2.jpg");// 把字节数组的图片写到另一个地方
if(!apple.exists()){
apple.createNewFile();
}
FileOutputStream fos = new FileOutputStream(apple);
fos.write(b);
fos.flush();
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace(); }
catch (IOException e) { e.printStackTrace();
}
}
}
}