0

当我从相机拍照时,我得到一张大小为 300-500 KB(1800 X 1700 像素)的图像。

但是当我从我的应用程序中拍照时,我会得到一个大小为 30-50 KB(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 倍。

谢谢你

4

3 回答 3

1

您需要创建新的位图对象并使用它Bitmap.createScaledBitmap()

Bitmap bit = (Bitmap) data.getExtras().get("data");
Bitmap bitmap =Bitmap.createScaledBitmap(bit, newWidth, newHeight, filter);
// use bitmap instead of bit object
于 2012-09-26T07:41:46.537 回答
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);
}
于 2012-09-26T07:41:53.543 回答
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()

于 2012-09-26T07:52:04.377 回答