用url从网络上下载一张图片(二进制),转换成BitMap格式的图片

//==========================用了两种方法======================
1.------------用流方式
try {
//实例化url
URL url = new URL(imgurl);
//载入图片到输入流
java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
//实例化存储字节数组
byte[] bytes = new byte[100];
//设置写入路径以及图片名称
OutputStream bos = new FileOutputStream(new File( "C:\\thetempimg.gif"));
int len;
while ((len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
bis.close();
bos.flush();
bos.close();
//关闭输出流
b=true;
} catch (Exception e) {
//如果图片未找到
b=false;
}

int[] a = new int[2];
if(b){ //图片存在
//得到文件
java.io.File file = new java.io.File("C:\\thetempimg.gif");
[color=darkblue]BufferedImage bi = null;[/color]
try {
//读取图片
[color=blue] bi = javax.imageio.ImageIO.read(file);[/color]
} catch (IOException ex) {
ex.printStackTrace();
}
a[0] = bi.getWidth(); //获得 宽度
a[1] = bi.getHeight(); //获得 高度
//删除文件
file.delete();
}else{ //图片不存在
a=null;
}
return a;

}


// 显示网络上的图片
public static Bitmap returnBitMap(String url) {
Log.i("returnBitMap","url="+url);
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
基于Javascript的bitmap处理,并且将位图输出为base64编码以便于浏览器进行显示。   一、Bitmap.create(width, height, bgcolor)     创建一个width x height像素大小的位图,底色为bgcolor所代表的颜色。     如:bitmap.create(10, 10, 0xff0000); // 创建一个10 x 10像素的底色为红色的位图 二、Bitmap.toBase64()     将位图输出为base64编码的带datauri头(data:image/bmp;base64,)的字符串,以便于在浏览器里显示。     如:document.getElementById('img1').src = bitmap.toBase64(); 三、Bitmap.fromBase64()     自图像的BASE64编码中恢复位图数据,目前只支持24位色的BMP位图数据。     如:bitmap.fromBase64('Qk06AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABgAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=='); 四、Bitmap.setBitmapBytes(val, idx, length)     修改bitmap位图数据的第idx位置起的length字节为val值。 五、Bitmap.getBitmapBytes(idx, length)     获取bitmap位图数据的第idx位置起的length个字节的值,返回值为数组。 六、Bitmap.setHeaderValue(attribute, headerValue)     设置attribute头属性的值为headerValue,attribute必须为BitMapFormat的成员属性,需要提供offset、length等属性值。     如:bitmap.setHeaderValue(BitmapFormat.biWidth, 500); // 设置位图的宽度为500像素值 七、Bitmap.getHeaderValue(attribute)     获取位图attribute头属性的值,attribute必须为BitmapFormat的成员属性,需要提供offset、length等属性值,返回的是经过Endian换后的实际整数值。 八、Bitmap.setPixel(x, y, color)     设置位图的(x, y)位置的像素值为color。 九、Bitmap.getPixel(x, y)     获取位图的(x, y)位置的RGB值,返回的内容为[ rr, gg, bb ]的数组内容 标签:jsBitmap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值