簡體   English   中英

Android在保存SD卡之前拍照並調整大小

[英]Android take photo and resize it before saving on sd card

我希望我的代碼在保存之前調整圖像大小,但我在Google上找不到任何關於它的信息。 請問你能幫幫我嗎 ?

這是代碼(來自Android doc):

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);

    picturePathForUpload = mCurrentPhotoPath;

    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

之后,我必須將其上傳到服務器。

非常感謝。

您可以在代碼后保存位圖圖像

Bitmap photo = (Bitmap) "your Bitmap image";
photo = Bitmap.createScaledBitmap(photo, 100, 100, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + "Imagename.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();

在閱讀了其他答案而沒有找到我想要的內容之后,這是我獲取適當​​縮放位圖的方法。 這是對Prabu答案的改編。

它確保您的圖片以不會使照片尺寸變形的方式縮放:

public saveScaledPhotoToFile() {
    //Convert your photo to a bitmap
    Bitmap photoBm = (Bitmap) "your Bitmap image";
    //get its orginal dimensions
    int bmOriginalWidth = photoBm.getWidth();
    int bmOriginalHeight = photoBm.getHeight();
    double originalWidthToHeightRatio =  1.0 * bmOriginalWidth / bmOriginalHeight;
    double originalHeightToWidthRatio =  1.0 * bmOriginalHeight / bmOriginalWidth;
    //choose a maximum height
    int maxHeight = 1024;
    //choose a max width
    int maxWidth = 1024;
    //call the method to get the scaled bitmap
    photoBm = getScaledBitmap(photoBm, bmOriginalWidth, bmOriginalHeight,
            originalWidthToHeightRatio, originalHeightToWidthRatio,
            maxHeight, maxWidth);

    /**********THE REST OF THIS IS FROM Prabu's answer*******/
    //create a byte array output stream to hold the photo's bytes
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    //compress the photo's bytes into the byte array output stream
    photoBm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //construct a File object to save the scaled file to
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "Imagename.jpg");
    //create the file
    f.createNewFile();

    //create an FileOutputStream on the created file
    FileOutputStream fo = new FileOutputStream(f);
    //write the photo's bytes to the file
    fo.write(bytes.toByteArray());

    //finish by closing the FileOutputStream
    fo.close();
}

private static Bitmap getScaledBitmap(Bitmap bm, int bmOriginalWidth, int bmOriginalHeight, double originalWidthToHeightRatio, double originalHeightToWidthRatio, int maxHeight, int maxWidth) {
    if(bmOriginalWidth > maxWidth || bmOriginalHeight > maxHeight) {
        Log.v(TAG, format("RESIZING bitmap FROM %sx%s ", bmOriginalWidth, bmOriginalHeight));

        if(bmOriginalWidth > bmOriginalHeight) {
            bm = scaleDeminsFromWidth(bm, maxWidth, bmOriginalHeight, originalHeightToWidthRatio);
        } else {
            bm = scaleDeminsFromHeight(bm, maxHeight, bmOriginalHeight, originalWidthToHeightRatio);
        }

        Log.v(TAG, format("RESIZED bitmap TO %sx%s ", bm.getWidth(), bm.getHeight()));
    }
    return bm;
}

private static Bitmap scaleDeminsFromHeight(Bitmap bm, int maxHeight, int bmOriginalHeight, double originalWidthToHeightRatio) {
    int newHeight = (int) Math.min(maxHeight, bmOriginalHeight * .55);
    int newWidth = (int) (newHeight * originalWidthToHeightRatio);
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
    return bm;
}

private static Bitmap scaleDeminsFromWidth(Bitmap bm, int maxWidth, int bmOriginalWidth, double originalHeightToWidthRatio) {
    //scale the width
    int newWidth = (int) Math.min(maxWidth, bmOriginalWidth * .75);
    int newHeight = (int) (newWidth * originalHeightToWidthRatio);
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
    return bm;
}

這是我的GitHub Gist的相應鏈接: https//gist.github.com/Lwdthe1/2d1cd0a12f30c18db698

首先將圖像轉換為位圖,然后使用以下代碼:

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

看到這將是完整的幫助。 一般情況下,如果您使用意圖通過相機拍攝圖像,您將獲得圖像的uri ,因此您將其讀作縮小圖像並將其存儲在同一位置

如果要捕獲全尺寸圖像,則別無選擇,只能將其保存到SD購物車,然后更改圖像的大小。

但是,如果縮略圖圖像足夠,則無需將其保存到SD卡,您可以從返回的意圖的附加內容中提取它。

您可以查看我為使用內置攝像頭Activity獲取圖像的兩種方法編寫的本指南:

指南:Android:對縮略圖和全尺寸圖像使用相機活動

BitmapFactory.Options optionsSignature = new BitmapFactory.Options();
final Bitmap bitmapSignature = BitmapFactory.decodeFile(
fileUriSignature.getPath(), optionsSignature);
Bitmap resizedSignature = Bitmap.createScaledBitmap(
                bitmapSignature, 256, 128, true);
signature.setImageBitmap(resizedSignature);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM