网络图片
final String gzhUrl ="http://cebqh.img48.wal8.com/img48/528379_20150921171826/153742157251.png";
// ivDialogbg.setImageResource(R.mipmap.gzgzh_ewm);
Glide.with(getActivity()).load(gzhUrl).placeholder(R.mipmap.erry_pic).error(R.mipmap.erry_pic).into(ivDialogbg);
Glide.with(getActivity()).load(gzhUrl).asBitmap().toBytes().into(new SimpleTarget() {
@Override
public void onResourceReady(byte[] bytes, GlideAnimation glideAnimation) {
try {
savaBitmaps(bytes);
}catch (Exception e) {
e.printStackTrace();
}
}
});
Bitmap 转byte[]
try { //本地图片
ByteArrayOutputStream output =new ByteArrayOutputStream();//初始化一个流对象
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);//把bitmap100%高质量压缩 到 output对象里
bitmap.recycle();//自由选择是否进行回收
byte[] result = output.toByteArray();//转换成功了
savaBitmap(result);
output.close();
// Result ss = parsePic(bitmap);//解析二维码
// Log.i("erwe", ss.getText().toString() + "");
}catch (Exception e) {
e.printStackTrace();
}
/**
* 保存方法
*
* @throws IOException
*/
public void savaBitmap(byte[] bytes) {//保存图片
String imgName = System.currentTimeMillis() +".jpg";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String filePath =null;
FileOutputStream fos =null;
try {
filePath = Environment.getExternalStorageDirectory() +"/DCIM/MyImg";
File imgDir =new File(filePath);
if (!imgDir.exists()) {
imgDir.mkdirs();
}
currentFile =new File(imgDir, imgName);
imgName = filePath +"/" + imgName;
fos =new FileOutputStream(imgName);
fos.write(bytes);
Log.i("picc", "" + imgName);
Toast.makeText(mContext, "图片已保存", Toast.LENGTH_SHORT).show();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fos !=null) {
fos.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}else {
Toast.makeText(mContext, "请检查SD卡是否可用", Toast.LENGTH_SHORT).show();
}
// 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
currentFile.getAbsolutePath(), imgName, null);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(currentFile.getPath()))));
}
public ResultparsePic(Bitmap bitmaps) {
//解析转换类型UTF-8
Hashtable hints =new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
//新建一个RGBLuminanceSource对象,将bitmap图片传给此对象
RGBLuminanceSource rgbLuminanceSource =new RGBLuminanceSource(bitmaps);
//将图片转换成二进制图片
BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(rgbLuminanceSource));
//初始化解析对象
QRCodeReader reader =new QRCodeReader();
//开始解析
Result result =null;
try {
result = reader.decode(binaryBitmap, hints);
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
public class RGBLuminanceSourceextends LuminanceSource {
private byte bitmapPixels[];
protected RGBLuminanceSource(Bitmap bitmap) {
super(bitmap.getWidth(), bitmap.getHeight());
// 首先,要取得该图片的像素数组内容
int[] data =new int[bitmap.getWidth() * bitmap.getHeight()];
this.bitmapPixels =new byte[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(data, 0, getWidth(), 0, 0, getWidth(), getHeight());
// 将int数组转换为byte数组,也就是取像素值中蓝色值部分作为辨析内容
for (int i =0; i < data.length; i++) {
this.bitmapPixels[i] = (byte) data[i];
}
}
@Override
public byte[]getMatrix() {
// 返回我们生成好的像素数据
return bitmapPixels;
}
@Override
public byte[]getRow(int y, byte[] row) {
// 这里要得到指定行的像素数据
System.arraycopy(bitmapPixels, y * getWidth(), row, 0, getWidth());
return row;
}
}