java将数据转为0到1_在java中将图像转换为二进制数据(0和1)

我想从URL读取图像并将其转换为二进制数据.请帮我..

byte[] data = null;

ByteArrayOutputStream bas = null;

try {

URL u = new URL(

"http://www.eso.org/public/archives/images/screen/eso0844a.jpg");

HttpURLConnection con1 = (HttpURLConnection) u.openConnection();

con1.setAllowUserInteraction(true);

con1.setRequestMethod("GET");

con1.connect();

InputStream is = con1.getInputStream();

BufferedImage imgToServe = null;

if (is != null) {

imgToServe = ImageIO.read(is);

}

bas = new ByteArrayOutputStream();

ImageIO.write(imgToServe, "jpg", bas);

File f = new File("C:\\img.jpg");

ImageIO.write(imgToServe, "jpg", f);

data = bas.toByteArray();

String str = "";

for (int i = 0; i < data.length; i++) {

str = str + toBinary(data[i]);

}

System.out.println(str);

} catch (HTTPException he) {

} catch (IOException ioe) {

}

}

private static String toBinary(byte b) {

StringBuilder sb = new StringBuilder("00000000");

for (int bit = 0; bit < 8; bit++) {

if (((b >> bit) & 1) > 0) {

sb.setCharAt(7 - bit, '1');

}

}

return (sb.toString());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值