2013-04-17 30 views
16

我希望我的代碼在保存前調整圖像大小,但在Google上找不到它。 你能幫我嗎?Android拍照並在保存在SD卡上之前調整大小

這是代碼(從Android電子文檔):

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

在那之後,我必須把它上傳到服務器。

非常感謝。

+0

喜弗洛裏安 您是否看過本頁面: http://stackoverflow.com/questions/12780375/resize-image-after-capture-it-from-native-camera-but-before-save-it-to-sd卡 Bonnejournée! :) –

回答

26

您可以保存位圖圖像下面的代碼

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(); 
+7

這將導致縮略圖和不是全尺寸的。 –

+1

也會影響圖像的清晰度 –

+0

鑑於上述評論,請看看我的答案:http://stackoverflow.com/a/36210688/4038790我認爲它更好地解決了您的問題。 – lwdthe1

4

首先轉換您的圖像以位圖,則使用此代碼:

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

謝謝@約翰!但是......我該如何保存它? –

+0

檢查此鏈接,這是非常相似的,你想要什麼:http://stackoverflow.com/questions/7687723/update-android-image-gallery-with-newly-created-bitmap – John

1

See this將有助於充分。一般來說,如果你是通過相機中使用的意圖拍攝的圖像,你會得到圖像的uri的結果你讀它的規模縮小圖像,並將其存儲在同一個地方

0

如果你想捕捉一個完整大小的圖像,那麼你別無選擇,只能將其保存到SD購物車,然後更改圖像的大小。

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

你可以看一下這個指南我寫的攝像頭Activity採取使用構建圖像兩種方法:

Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

4

閱讀其他的答案,並沒有找到正是我想要的以後,這裏是我的方法獲取適當縮放的位圖。這是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的要點是:https://gist.github.com/Lwdthe1/2d1cd0a12f30c18db698

+1

如果照片是方形(寬度==高度),則您的方法getScaledBitmap(...)將避免縮放。你需要刪除這一行:START OF LINE: else if(bmOriginalHeight> bmOriginalWidth){END of LINE and just use else { –

+1

你不應該使用Math.min嗎? – lionheart

+0

@UdiReshef好抓。你也是獅心。固定 – lwdthe1

0
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);