bitmap图像工具
2016-07-28 14:04
417 查看
1、转换图片为bitmap对象
2、把图片bitmap转换成字符流byte
3、吧byte字节转换图片
4、吧byte转换图片保存本机
5、获取文件夹下的所有图片路径
6、对图片进行修改操作
7、缩放图片
public static Bitmap getToBitmap(String path, int w, int h) { BitmapFactory.Options opts = new BitmapFactory.Options(); // 设置为ture只获取图片大小 opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.ARGB_8888; // 返回为空 BitmapFactory.decodeFile(path, opts); int width = opts.outWidth; int height = opts.outHeight; float scaleWidth = 0.f, scaleHeight = 0.f; if (width > w || height > h) { // 缩放 scaleWidth = ((float) width) / w; scaleHeight = ((float) height) / h; } opts.inJustDecodeBounds = false; float scale = Math.max(scaleWidth, scaleHeight); opts.inSampleSize = (int) scale; WeakReference<Bitmap> weak = new WeakReference<Bitmap>( BitmapFactory.decodeFile(path, opts)); return Bitmap.createScaledBitmap(weak.get(), w, h, true); }
2、把图片bitmap转换成字符流byte
public static byte[] bitmap2Bytes(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
3、吧byte字节转换图片
public static Bitmap Bytes2Bitmap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } }
4、吧byte转换图片保存本机
public static void writeImageToDisk(byte[] img, String fileName) { try { String path=Environment.getExternalStorageDirectory().toString() + File.separator + "image/"; File file = new File(path); if(!file.exists()){ file.mkdirs(); } File photoFile=new File(path,fileName+".png"); FileOutputStream fops = new FileOutputStream(photoFile); fops.write(img); fops.flush(); fops.close(); } catch (Exception e) { e.printStackTrace(); } }
5、获取文件夹下的所有图片路径
public static List<String> getImagePath() { List<String> imagePathList = new ArrayList<String>(); String filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "image"; File fileAll = new File(filePath); File[] files = fileAll.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; if (checkIsImageFile(file.getPath())) { imagePathList.add(file.getPath()); } } return imagePathList; } //检查扩展名,得到图片格式的文件 private static boolean checkIsImageFile(String fName) { boolean isImageFile = false; String FileEnd = fName.substring(fName.lastIndexOf(".") + 1,fName.length()).toLowerCase(); if (FileEnd.equals("jpg") || FileEnd.equals("png") || FileEnd.equals("gif") || FileEnd.equals("jpeg")|| FileEnd.equals("bmp") ) { isImageFile = true; } else { isImageFile = false; } return isImageFile; }
6、对图片进行修改操作
public static Bitmap revitionImageSize(String path) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream( new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; Bitmap bitmap = null; while (true) { if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) { in = new BufferedInputStream( new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } return bitmap; }
7、缩放图片
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { if (bitmap == null) return bitmap; int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); if (scaleWidth < scaleHeight) { matrix.postScale(scaleWidth, scaleWidth); } else { matrix.postScale(scaleHeight, scaleHeight); } Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories