Android 根据图片路径生成新图片

此博客为转载内容,原链接为https://www.cnblogs.com/Ayinger/p/11077850.html ,涉及移动开发和人工智能领域。
/**
 * 新图片保存路径
 * @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;
}

 

转载于:https://www.cnblogs.com/Ayinger/p/11077850.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值