2012-09-26 139 views
0

當我從相機拍攝照片時,我得到的圖像大小爲300-500 K.B(1800 X 1700像素)。Android:將位圖的大小調整爲更大的像素寬度和高度

但是,當我從我的應用程序拍照時,我得到一個大小爲30-50 K.B(150 X 200像素)的圖像。

如何將150 x 200像素大小調整爲像300 x 400像素?

因爲我發送此圖像的服務器將不接受小於280 x 360像素尺寸的圖像。

我現在使用的代碼是:

Bitmap bit = (Bitmap) data.getExtras().get("data"); 
ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
bit.compress(Bitmap.CompressFormat.JPEG, 100, bao); 
byte[] ba = bao.toByteArray(); 
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My - Images"); 
File f = new File(imagesFolder, "test.jpg"); 
f.createNewFile(); 
FileOutputStream fo = new FileOutputStream(f); 
fo.write(ba); 
fo.flush(); 
fo.close(); 

我沒有壓縮的圖像,但我仍然獲得10次較小的圖像尺寸相比圖像使用相機從主屏幕取。

謝謝

回答

1

要獲得全尺寸的攝像機圖像,你應該指向相機保存圖片在一些臨時文件。那麼只有你可以得到一個實際大小的圖像。

intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
File photo; 
try 
{ 
    // place where to store camera taken picture 
    photo = this.createTemporaryFile("picture", ".jpg"); 
    photo.delete(); 
} 
catch(Exception e) 
{ 
    Log.v(TAG, "Can't create file to take picture!"); 
    Toast.makeText(activity, "Please check SD card! Image shot is impossible!", 10000); 
    return false; 
} 
mImageUri = Uri.fromFile(photo); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); 
//start camera intent 
activity.startActivityForResult(this, intent, MenuShootImage); 

private File createTemporaryFile(String part, String ext) throws Exception 
{ 
File tempDir= Environment.getExternalStorageDirectory(); 
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/"; 
if(!tempDir.exists()) 
{ 
    tempDir.mkdir(); 
} 
return File.createTempFile(part, ext, tempDir); 
} 

再經過圖像拍攝意圖完成工作 - 只是用下面的代碼抓住從imageUri您的圖片:

public void grabImage(ImageView imageView) 
{ 
this.getContentResolver().notifyChange(mImageUri, null); 
ContentResolver cr = this.getContentResolver(); 
Bitmap bitmap; 
try 
{ 
    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri); 
    imageView.setImageBitmap(bitmap); 
} 
catch (Exception e) 
{ 
    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); 
    Log.d(TAG, "Failed to load", e); 
} 
} 


//called after camera intent finished 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
if(requestCode==MenuShootImage && resultCode==RESULT_OK) 
{ 
    ImageView imageView; 
    //... some code to inflate/create/find appropriate ImageView to place grabbed image 
    this.grabImage(imageView); 
} 
super.onActivityResult(requestCode, resultCode, intent); 
} 
+0

我已經將它保存在SD卡上。我應該將它保存爲臨時文件嗎?現在它永久保存 –

1

您需要創建新的位圖對象,並使用該使用Bitmap.createScaledBitmap()

Bitmap bit = (Bitmap) data.getExtras().get("data"); 
Bitmap bitmap =Bitmap.createScaledBitmap(bit, newWidth, newHeight, filter); 
// use bitmap instead of bit object 
+0

是的。我結束了使用這個代碼。但是,你能告訴我爲什麼我得到這樣一個低尺寸的圖像相比,從相機拍攝的圖像? –

+0

用這種方法嘗試不創建縮放的位圖bit.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(new File(「f」))); – Pratik

0

在你拍照,你應該配置攝像頭。

第一

camera.open(); 

檢查支持的圖片尺寸:

Camera.Parameters p = camera.getParameters(); 
    List<Size> ss = p.getSupportedPictureSizes(); 
    Iterator<Size> it = ss.iterator(); 
    Size sizeIWant = null; 
    while(it.hasNext()){ 
    Size s = it.next(); 
     <compute sizeIWant based on various sizes provided> 
    } 
    p.setPictureSize(sizeIWant.width, sizeIWant.height); 
    camera.setParameters(p); 

然後拍攝(代碼),不要忘記調用camera.release()

相關問題