2012-09-26 141 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()

相关问题