Android图片转为16进制

本文介绍了一种方法,能够将图片转换为十六进制字符串。该方法首先将图片加载到ImageView中,然后通过BitmapDrawable获取Bitmap对象。接着使用Bitmap.CompressFormat.JPEG进行压缩,并最终转换为十六进制字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 将图片转换成十六进制字符串
* @param photo
* @return
*/
public static String sendPhoto(ImageView photo) {
Drawable d = photo.getDrawable();
Bitmap bitmap=((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);// (0 - 100)压缩文件
byte[] bt = stream.toByteArray();
String photoStr = byte2hex(bt);
return photoStr;


}
/**
* 二进制转字符串
* @param b
* @return
*/
public static String byte2hex(byte[] b)  
{
StringBuilder sb = new StringBuilder();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
sb.append("0" + stmp);
} else {
sb.append(stmp);
}


}
return sb.toString();
}

  

转载于:https://www.cnblogs.com/xxdc/p/3954332.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值