zoukankan      html  css  js  c++  java
  • Android 根据图片路径生成新图片

    /**
     * 新图片保存路径
     * @param oldPicPath
     * @param isCover
     * @return
     */
    private String createCompressPic(String oldPicPath,boolean isCover) {
        if(TextUtils.isEmpty(oldPicPath)){
            return "";
        }
        if(!obtainPicSize(oldPicPath)){
            return "";
        }
        Bitmap bitmap = getCompressBitmap(oldPicPath);
        String picPath ="";
        File file;
        if(isCover){ // 覆盖原图
            picPath = oldPicPath;
        }else {
            picPath =  oldPicPath.substring(0,oldPicPath.lastIndexOf(".")) +"_dis.jpg";
        }
        file = new File(picPath);
        if(file.exists()){
            file.delete();
        }
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
    /***
     * 动态获取图像的宽高
     * @param recogPicPath 识别图像路径
     */
    private boolean  obtainPicSize(String recogPicPath) {
        try {
            File file = new File(recogPicPath);
            BitmapFactory.Options options = null;
            if (!file.exists()) {
                Toast.makeText(context, "读取文件不存在",
                        Toast.LENGTH_LONG).show();
                return false;
            }
            options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(recogPicPath, options);
            if (options == null) {
                return false;
            }
            curPicWidth = options.outWidth;
            curPicHeight = options.outHeight;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    /**
     * 生成bitmap数据
     * @param path
     * @return
     */
    public Bitmap getCompressBitmap(String path){
        Bitmap bitmap = null;
        bitmap = BitmapFactory.decodeFile(path);
        if(bitmap != null){
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, curPicWidth, curPicHeight);
        }
        return bitmap;
    }
  • 相关阅读:
    在ubuntu下安装phpmyadmin 出现404错误
    Jquery插件收藏
    PHP 时区设置
    Jquery动态进行图片缩略
    CSS设置图片垂直居中的方法
    解决CI框架的Disallowed Key Characters错误提示
    @Component默认是单例还是多例?
    ehcache的heap、off-heap、desk浅谈
    nginx负载均衡分配策略有哪些?
    HashSet的实现原理,简单易懂
  • 原文地址:https://www.cnblogs.com/Ayinger/p/11077850.html
Copyright © 2011-2022 走看看