Android(安卓)Bitmap常见用法
16lz
2021-01-26
文章目录
- @[toc]
- 1. 摄像头byte[]数据转为bitmap
- 2. bitmap转为byte[]数据
- 3. bitmap保存为图片
- 4. byte[]直接保存为图片
- 5. 从图片得到Bitmap
- 6. bitmap的裁剪
- 7. 从resource得到bitmap
- 参考链接
- @[toc]
- 1. 摄像头byte[]数据转为bitmap
- 2. bitmap转为byte[]数据
- 3. bitmap保存为图片
- 4. byte[]直接保存为图片
- 5. 从图片得到Bitmap
- 6. bitmap的裁剪
- 7. 从resource得到bitmap
- 参考链接
1. 摄像头byte[]数据转为bitmap
正确方法
public static Bitmap saveBGRA8888ByteToBitmap(byte[] data, int width, int height) { byte[] bgraData = new byte[width*height*4]; System.arraycopy(data, 0, bgraData, 0, width*height*4); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); ByteBuffer buf = ByteBuffer.wrap(bgraData); bitmap.copyPixelsFromBuffer(buf); return bitmap;}
这种方法在摄像头会出现getwidth NPE应该byte[]不包含这些数据
byte[] b = getIntent().getByteArrayExtra("bitmap"); Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
2. bitmap转为byte[]数据
int bytes = bmp.getByteCount();ByteBuffer buf = ByteBuffer.allocate(bytes);bmp.copyPixelsToBuffer(buf);byte[] byteArray = buf.array();
3. bitmap保存为图片
/* * 保存bitmap到图片,PNG可以选择其他格式JPE PNG * @param bitmap原图 * @param savaPath保存文件的路径 */public static void saveBitmapToJpg(Bitmap bitmap, String savePath) { File file=new File(savePath); try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }} @frameworks/base/graphics/java/android/graphics/Bitmap.javapublic enum CompressFormat { JPEG (0), PNG (1), WEBP (2); CompressFormat(int nativeInt) { this.nativeInt = nativeInt; } final int nativeInt;}
4. byte[]直接保存为图片
/* *把BGRA格式的byte[]数据保存到图片 *@param data: 摄像头传过来的图像数据 *@param width: 图片的宽度 *@param height: 图片的高度 *@param savaPath保存文件的路径 */public static void saveBGRA8888ToPicture(byte[] data, int width, int height, String savePath) { byte[] bgraData = new byte[width*height*4]; System.arraycopy(data, 0, bgraData, 0, width*height*4); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); ByteBuffer buf = ByteBuffer.wrap(bgraData); bitmap.copyPixelsFromBuffer(buf); File file=new File(savePath); if(file.exists()){ file.delete(); } try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } bitmap.recycle();}//适用于yuv格式的byte[]保存public static boolean saveYuvByteToJpegFile(byte[] bytes, int width, int height, String oldName){ FileOutputStream fileOutputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; Bitmap bitmap = null; String filePath = EXTERNAL_FILES_PATH + "/sensetime" + "/" + oldName + ".jpeg"; File fileDir = new File(EXTERNAL_FILES_PATH + "/sensetime"); if(!fileDir.exists() && !fileDir.mkdir()){ return false; } File file = new File(filePath); try{ YuvImage yuvImage = new YuvImage(bytes, ImageFormat.NV21, width, height, null); byteArrayOutputStream = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, byteArrayOutputStream); bitmap = BitmapFactory.decodeByteArray(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size()); if(!file.exists() && !file.createNewFile()) return false; fileOutputStream = new FileOutputStream(file); return bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); }catch (Exception e){ Log.e(TAG, e.toString()); }finally { try{ if(fileOutputStream != null){ fileOutputStream.flush(); fileOutputStream.close(); } if(byteArrayOutputStream != null){ byteArrayOutputStream.flush(); byteArrayOutputStream.close(); } if(bitmap != null){ bitmap.recycle(); bitmap = null; } }catch (Exception e){ e.printStackTrace(); } } return false;}
5. 从图片得到Bitmap
会通过inSampleSize的值来压缩分辨率
public static Bitmap getImageResource(String imagePath){ Bitmap bitmap = null; try{ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeFile(imagePath, options); int previewWidth = options.outWidth; int previewHeight = options.outHeight; options.inJustDecodeBounds = false; if(previewWidth > 2560 || previewHeight > 2560) options.inSampleSize = 8; else if(previewWidth > 1280 || previewHeight > 1280) options.inSampleSize = 4; else if(previewWidth > 640 || previewHeight > 640) options.inSampleSize = 2; options.inPreferredConfig = Bitmap.Config.ARGB_8888; bitmap = BitmapFactory.decodeFile(imagePath, options); }catch (Exception e){ e.printStackTrace(); } return bitmap;}
6. bitmap的裁剪
//基本裁剪接口 public static Bitmap cropBitmap(Bitmap bitmap, int x, int y, int width, int height) { return Bitmap.createBitmap(bitmap, x, y, width, height);}/* *@param bitmap: 原图 *@param x,y: 截取框第一个点的的xy坐标 *@param width: 截取bitmap的宽度 *@param height: 截取bitmap的高度 *@param newWidth newHeight 按上面参数截取后在压缩到newWidth newHeight尺寸 */public static Bitmap cropBitmap(Bitmap bitmap, int x, int y, int width, int height, int newWidth , int newHeight) { Matrix matrix = new Matrix(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Log.e(TAG, "cropBitmap scaleWidth: " + scaleWidth + " scaleHeight: " + scaleHeight); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, x, y, width, height, matrix, true );}
7. 从resource得到bitmap
bgra.jpg是width*heihgt 1280×720图片,在车技上得到bitmap的高度上正确的,而在pixel手机上为:
bitmap width: 3360 height:1890被拉伸了,要注意这种情况
UseBitmap(this, imageView , R.drawable.bgra);// 1.从资源中获取Bitmappublic void UseBitmap(Context context, ImageView imageView, int drawableId) { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId); Log.d(TAG, "bitmap width: " + bitmap.getWidth() + " height:" + bitmap.getHeight()); Bitmap mbitmap = Bitmap.createBitmap(bitmap, 0 , 0 ,700 ,700); Bitmap mmbitmap = Bitmap.createBitmap(mbitmap, 0 , 0 ,300 ,300); Bitmap mmmbitmap = Bitmap.createBitmap(mmbitmap, 0 , 0 ,200 ,100); imageView.setImageBitmap(mmmbitmap); bitmap.recycle(); //mbitmap.recycle();}
参考链接
1. 开源一个 Android 图片压缩框架
2. Android Bitmap 和 ByteArray的互相转换
3. Android Bitmap最全面详解
更多相关文章
- Android(安卓)图片压缩
- Android学习笔记01_走马观花
- Android(安卓)BaseAdapter使用介绍
- Android(安卓)微信分享图片!!!
- Android(安卓)蓝牙搜索,配对,连接发送数据
- Android客户端和Struts交互Json数据
- 【Android】利用ArrayAdapter/SimpleAdapter创建ListView
- Android(安卓)Studio查看数据库插件
- Android(安卓)google VR全景图导航