【问题标题】:Android: resize a bitmap to larger pixel width and heightAndroid:将位图调整为更大的像素宽度和高度
【发布时间】:2012-09-26 07:23:52
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: android image android-camera


    【解决方案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);
    }
    

    【讨论】:

    • 我已经将它保存在 SD 卡上。我应该将它保存为临时文件吗?现在它已永久保存
    【解决方案2】:

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

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

    【讨论】:

    • 是的。我最终使用了这段代码。但是你能告诉我为什么我得到的图像与从相机拍摄的图像相比尺寸如此之小吗??
    • 尝试这种方式而不创建缩放位图 bit.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("f")));
    【解决方案3】:

    在拍照之前,您应该配置相机。

    第一

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

    【讨论】:

      猜你喜欢
      • 2014-04-04
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      相关资源
      最近更新 更多