图片旋转,压缩,保存,拷贝+dp2px
来源:互联网 发布:安卓手机运行java应用 编辑:程序博客网 时间:2024/06/16 07:25
/** * 选择变换 * * @param origin 原图 * @param alpha 旋转角度,可正可负 * @return 旋转后的图片 */ public Bitmap rotateBitmap(Bitmap origin, float alpha) { if (origin == null) { return null; } int width = origin.getWidth(); int height = origin.getHeight(); Matrix matrix = new Matrix(); matrix.setRotate(alpha); // 围绕原地进行旋转 Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false); if (newBM.equals(origin)) { return newBM; } origin.recycle(); return newBM; } /** * 图片缩放 * * @param bitmap 原图 * @param rate 缩放比例,例如0.5--表示压缩为原图的1/2 * @return 缩放后图片 */ private static Bitmap smallBitmap(Bitmap bitmap, float rate) { Matrix matrix = new Matrix(); matrix.postScale(rate, rate); //长和宽放大缩小的比例 Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); bitmap = null; return resizeBmp; } private int i = 1; private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/0Face/"; private Calendar now = new GregorianCalendar(); private SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()); private String fileName = simpleDate.format(now.getTime()); public void saveBitmap(Bitmap mBitmap) { File dir = new File(path); if (!dir.exists()) dir.mkdirs(); File f = new File(path + (fileName + "-" + i++) + ".jpg"); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); } catch (IOException e) { e.printStackTrace(); Log.e("FFF", e.getMessage()); } } /** * 根据原位图生成一个新的位图,并将原位图所占空间释放 * * @param srcBmp 原位图 * @return 新位图 */ public static Bitmap copy(Bitmap srcBmp) { Bitmap destBmp = null; try { //创建一个临时文件 File file = new File("/mnt/sdcard/temp/tmp.txt"); file.getParentFile().mkdirs(); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); int width = srcBmp.getWidth(); int height = srcBmp.getHeight(); FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, width * height * 4); //将位图信息写进buffer srcBmp.copyPixelsToBuffer(map); //释放原位图占用的空间 srcBmp.recycle(); //创建一个新的位图 destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); map.position(0); //从临时缓冲中拷贝位图信息 destBmp.copyPixelsFromBuffer(map); channel.close(); randomAccessFile.close(); } catch (Exception ex) { destBmp = null; } return destBmp; } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(float dpValue) { final float scale = PhoneApplication.getApplication().getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(float pxValue) { final float scale = PhoneApplication.getApplication().getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 获得屏幕宽度 * * @return */ public static int getScreenWidth() { WindowManager wm = (WindowManager) PhoneApplication.getApplication() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 获得屏幕高度 * * @return */ public static int getScreenHeight() { WindowManager wm = (WindowManager) PhoneApplication.getApplication() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; }
0 0
- 图片旋转,压缩,保存,拷贝+dp2px
- 压缩图片,保存图片
- 代码压缩旋转图片
- dp2px
- 旋转图片并保存
- 图片压缩保存 处理三星拍照图片旋转问题的部分方法
- IOS 图片压缩保存
- 图片压缩保存
- android 图片压缩,自动旋转保存,查看照片信息,拍照设备类型等
- 常用的图片相关方法,读取,保存,压缩,缩放,旋转,drawable转化
- HTML5图片压缩旋转调整
- 上传图片进行压缩保存
- 图片压缩保存读取操作
- 图片压缩旋转大全(质量压缩,比列压缩)
- Thumbnailator实现图片压缩,旋转,添加水印
- 拍照/选择相册,图片压缩旋转处理
- AndroidUtils(五)Android图片压缩、旋转工具
- java实现图片旋转保存到数据库
- redhat6.5安装postgre9.2
- SQLITE3 使用总结(十)
- java数组工具类
- java-----------华为机试----------求int型正整数在内存中存储时1的个数 (水题for java)
- Notes on Tensorflow(四)Variables
- 图片旋转,压缩,保存,拷贝+dp2px
- 2017-04-28欠博客一篇
- mysql下载、安装、配置
- 2017-04-28欠博客一篇
- hdu 1032 The 3n+1 problem
- 题解——Leetcode 6. ZigZag Conversion 难度:Medium
- 2017-04-28欠博客一篇
- Ubuntu 查看端口占用情况
- linux command