天天看點

Android圖檔處理

Android圖檔處理

圖檔儲存

public static final String IMAGE_UNSPECIFIED = "image/*";
/**
 * 将圖檔存儲至SD卡,需判斷是否裝有SD卡、是否可讀寫、是否有空間,否則提示出錯
 * @param ctx 上下文
 * @param jpeg 要存儲的照片
 * @param quality 壓縮照片的品質,0至100,100最佳,一般80-90
 * @param filePath 存儲的路徑
 * @param filename 照片的名稱
 * @return
 */
public static boolean save_picture(Context ctx, Bitmap bitmap, int quality, String filePath, String filename) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    byte[] data = baos.toByteArray();

    if (!Common.checkSDStatus(data.length//)) {
        Toast.makeText(ctx, "您的儲存卡有錯誤", Toast.LENGTH_SHORT).show();
        return false;
    }

    try {
        File destDir = new File(filePath);
        if (!destDir.exists())
            destDir.mkdirs();

        String path = filePath + "/" + filename;
        File file = new File(path);
        if (!file.exists())
            file.createNewFile();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}
           

圓角圖檔

/**
 * 獲得圓角圖檔的方法
 * @param bitmap 需處理的圖檔
 * @param roundPx 圓角的弧率
 * @return
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = ;
    final Paint paint = new Paint();
    final Rect rect = new Rect(, , bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(, , , );
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}
           

在圖檔上寫字

/**
 * 圖檔中繪入GPS和時間等文字
 * @param bitmap 需處理的圖檔
 * @param datetime 時間
 * @param lat 經度
 * @param lng 緯度
 * @return
 */
public static Bitmap getGpsBitmap(Bitmap bitmap, String datetime, String lat, String lng) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    /* 把位圖寫進畫布canvas類 */
    Canvas canvas = new Canvas(output);
    /* 畫布的區域 */
    final Rect rect = new Rect(, , bitmap.getWidth(), bitmap.getHeight());
    /* 噴漆Paint類 */
    Paint paint = new Paint();
    paint.setAntiAlias(true);//消除鋸齒
    paint.setColor(Color.RED);//着色
    paint.setTextSize();//字型大小
    canvas.drawText("經度:" + lng, , , paint);
    canvas.drawText("緯度:" + lat, , , paint);
    canvas.drawText("時間:" + datetime, , , paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
           

圖檔剪裁

/**
 * 裁切圖檔
 * @param originFile 源檔案
 * @param TargetFile 目标檔案
 * @param aspect 寬高比例,如果為null,則不限制
 * @param output 輸出分辨率
 * @return
 */
public static Intent startPhotoZoom(File originFile, File TargetFile, int[] aspect, int[] output) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(Uri.fromFile(originFile), IMAGE_UNSPECIFIED);
    intent.putExtra("crop", "true");
    intent.putExtra("noFaceDetection", true);
    intent.putExtra("return-data", false);

    if (null != output) {
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(originFile.getPath(), op);

        int jpgWidth = op.outWidth;
        int jpgHeight = op.outHeight;

        if (jpgWidth > output[] && jpgHeight > output[]) {
            intent.putExtra("outputX", output[]);
            intent.putExtra("outputY", output[]);
        }
    }

    if (null != aspect) {
        intent.putExtra("aspectX", aspect[]);
        intent.putExtra("aspectY", aspect[]);
    }

    if (!TargetFile.exists())
        try {
            TargetFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(TargetFile));
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

    return intent;
}
           

圖檔擷取

/**
 * 從相冊中選擇一張照片之後,擷取該照片的絕對路徑
 * @param ctx
 * @param photoUri
 * @return
 */
public static String getPickPhotoPath(Context ctx, Uri photoUri) {
    Cursor cursor = null;
    try {
        cursor = ctx.getContentResolver().query(photoUri, null, null, null, null);
        cursor.moveToFirst();
        String imgPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

        return imgPath;
    } catch (Exception e) {
        return "";
    } finally {
        cursor.close();
    }
}

public static File getPickPhotoFile(Context ctx, Uri photoUri) {
    String imgPath = getPickPhotoPath(ctx, photoUri);
    if (!TextUtils.isEmpty(imgPath))
        return new File(imgPath);
    else
        return null;
}
           

圖檔壓縮

/**
 * 壓縮圖檔大小,避免圖檔過大,保持比例不變,寬或高不超過XX個像素
 * @param newName 新的檔案名稱
 * @param filePath 原檔案全路徑,包含檔案名
 * @param attachPath 處理過後,檔案存放的位置
 * @param String 新的檔案全路徑
 */
public static String compressPixelPhotos(final Context ctx, final String newName, final String filePath,
        final String attachPath) {
    BitmapFactory.Options op = new BitmapFactory.Options();
    op.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, op);

    int jpgWidth = op.outWidth;
    int jpgHeight = op.outHeight;

    if (jpgWidth >  || jpgHeight > ) {
        int wSendRatio = (int) Math.ceil(jpgWidth / f);
        int hSendRatio = (int) Math.ceil(jpgHeight / f);
        if (wSendRatio >  && hSendRatio > ) {
            op.inSampleSize = wSendRatio > hSendRatio ? wSendRatio : hSendRatio;
        }
        op.inJustDecodeBounds = false;
        Bitmap b = BitmapFactory.decodeFile(filePath, op);

        if (!save_picture(ctx, b, , attachPath, newName)) {
            Common.copyFileToFile(filePath, attachPath + File.separator + newName);
        }

        if (b != null && !b.isRecycled())
            b.recycle();

    } else {
        Common.copyFileToFile(filePath, attachPath + File.separator + newName);
    }

    return attachPath + File.separator + newName;
}

/**
 * 檢查圖檔分辨率大小,是否需要壓縮
 * @param ctx
 * @param filePath
 * @return
 */
public static boolean compressPixelPhotosCheck(final Context ctx, final String filePath) {
    BitmapFactory.Options op = new BitmapFactory.Options();
    op.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, op);

    if (op.outWidth >  || op.outHeight > ) {
        return true;
    } else {
        return false;
    }
}

/**
 * 将
 * @param filename 檔案名,全路徑
 * @param jpgGetWidth 照片寬
 * @param jpgGetHeight 照片高
 * @return
 */
public static Bitmap decodeFile(String filename, int jpgGetWidth, int jpgGetHeight) {
    Bitmap b = null;
    try {
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, op);

        int jpgWidth = op.outWidth;
        int jpgHeight = op.outHeight;

        int wSendRatio = (int) Math.ceil(jpgWidth / Double.valueOf(jpgGetWidth));
        int hSendRatio = (int) Math.ceil(jpgHeight / Double.valueOf(jpgGetHeight));
        if (wSendRatio >  && hSendRatio > ) {
            op.inSampleSize = wSendRatio > hSendRatio ? wSendRatio : hSendRatio;
        }
        op.inJustDecodeBounds = false;
        b = BitmapFactory.decodeFile(filename, op);

    } catch (Exception e) {
    }
    return b;
}