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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM