图片旋转,压缩,保存,拷贝+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