j2me图片转换为二进制的解决办法?
来源:互联网 发布:大数据为什么在贵阳 编辑:程序博客网 时间:2024/04/30 16:09
/****
* @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];
}
return new String(out);
}
* @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];
}
return new String(out);
}
- j2me图片转换为二进制的解决办法?
- 图片的二进制转换
- flex 显示二进制图片及把图片转换为二进制
- 转换为二进制的补码
- 存储过程中的二进制图片数据转换为图片文件
- 获取地址栏的IP和端口,将URL地址图片转换为二进制
- 关于图片验证码返回二进制流,进行转换为Web的相对路径
- Docx4j将word中的图片转换为二进制流
- 使用OpenCV把二进制mnist数据集转换为图片
- c#--将图片转换为二进制序列字符串
- 转换十进制为二进制的函数
- 十进制转换为二进制的简便算法
- 十进制转换为二进制的方法
- 二进制的串转换为整数
- 将十进制的168转换为二进制
- 二进制转换为十进制的小程序
- 十进制转换为二进制的小程序
- 二进制转换为十进制的简便方法
- sitemesh 项目中的使用
- ORACLE SQL Loader的详细语法
- setjmp和longjmp的"非本地跳转"
- 记录一些好文章.
- wpf app.config 客户端进行配置和修改.
- j2me图片转换为二进制的解决办法?
- 深刻理解Linux进程间通信(IPC)
- RAC架构之业务分割
- delphi 中实现当期日期 减去 若干小时的方法
- spring中的Bean配置注意的地方
- Solaris quota命令
- uvaoj 488 - Triangle Wave
- 用汇编编写DOS下的内存驻留程序
- 列出一个HTML文件的所有标签