【问题标题】:Resize Image taken from Camera before being Saved在保存之前调整从相机拍摄的图像大小
【发布时间】:2015-11-18 08:45:45
【问题描述】:

我正在使用内置相机应用程序拍照,并获得图像分辨率 (1600 x 1200) 但我想将 (1200 x 900) 中的所有图像保存到 SD 卡中,因为我已经编写了一个方法但是仍然获得原始大小的图像。

这是我的代码,我用它来捕获图像并将其存储到 SD 卡中

public class FormActivity extends AppCompatActivity {

String filePath = null;
File file;
Uri output;
final int requestCode = 100;

String stringImageName= null;

static int w = 1200;
static int h = 900;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_form);     

    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
    stringImageName = s.format(new Date());
    Log.d("format::", stringImageName);

    filePath = Environment.getExternalStorageDirectory() + "/"+stringImageName+".jpeg";
    file = new File(filePath);
    output = Uri.fromFile(file);


    buttonOrderNow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            photoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);                       
            startActivityForResult(photoCaptureIntent, requestCode);

        }
    });

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK) {

         resize();

    }
}

private void resize() {

    Long startTime = System.currentTimeMillis();

    Bitmap bitmap_Source = BitmapFactory.decodeFile(filePath);
     float factorH = h / (float)bitmap_Source.getHeight();
     float factorW = w / (float)bitmap_Source.getWidth();
     float factorToUse = (factorH > factorW) ? factorW : factorH;
     Bitmap bm = Bitmap.createScaledBitmap(bitmap_Source, 
       (int) (bitmap_Source.getWidth() * factorToUse), 
       (int) (bitmap_Source.getHeight() * factorToUse), 
       false);      

     Long endTime = System.currentTimeMillis();
     Long processTime = endTime - startTime;
     Toast.makeText(FormActivity.this, ""+processTime, Toast.LENGTH_LONG).show();

    }

【问题讨论】:

  • 可能,这个问题被否决了,因为您没有尝试解决您的问题。即使您查看右侧的“相关”问题列表,您也会看到Resize image taken from gallery or camera, before being uploaded,它提供了一个可行的答案。
  • @AlexCohn 检查我更新的代码,我已经尝试过了,但问题仍然没有解决
  • 裁剪确实将您的图像调整为小 KB stackoverflow.com/questions/29532914/…
  • 现在缺少什么?缩放后的图像没有写回文件?
  • 顺便说一句,请确保您永远不要放大图像!

标签: android android-intent bitmap camera image-resizing


【解决方案1】:

我猜这个, 你应该传递值高度和宽度 传递您的图像路径并获得准确的图像

private Bitmap getBitmap(String path) {

Uri uri = getImageUri(path);
InputStream in = null;
try {
    final int IMAGE_MAX_SIZE = 1920000; // 1.9MP
    in = mContentResolver.openInputStream(uri);
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, o);
    in.close();
    int scale = 1;
    while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
          IMAGE_MAX_SIZE) {
       scale++;
    }
    Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", 
       orig-height: " + o.outHeight);
    Bitmap b = null;
    in = mContentResolver.openInputStream(uri);
    if (scale > 1) {
        scale--;
        // scale to max possible inSampleSize that still yields an image
        // larger than target
        o = new BitmapFactory.Options();
        o.inSampleSize = scale;
        b = BitmapFactory.decodeStream(in, null, o);

        // resize to desired dimensions
        int height = b.getHeight(); 
        int width = b.getWidth();
        Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
           height: " + height);

        double y = Math.sqrt(IMAGE_MAX_SIZE
                / (((double) width) / height));
        double x = (y / height) * width;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
           (int) y, true);
        b.recycle();
        b = scaledBitmap;

        System.gc();
    } else {
        b = BitmapFactory.decodeStream(in);
    }
    in.close();

    Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
       b.getHeight());
    return b;
} catch (IOException e) {
    Log.e(TAG, e.getMessage(),e);
    return null;
}

【讨论】:

    【解决方案2】:

    取自here

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if(this.requestCode == requestCode && resultCode == RESULT_OK){  
            Bitmap yourBitmap= (Bitmap) data.getExtras().get("data"); 
            //Resize it as you need
            Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 1200, 900, true);
    
            //Now you can save it
        }  
    } 
    

    【讨论】:

    • -1 因为 OP 想要在保存图像之前调整图像大小。在onActivityResult() 图片已经保存。此外,您的代码加载了缩略图,而不是原始大小,所以这也是错误的。见:developer.android.com/training/camera/…
    • @Bevor 错误的是“在 onActivityResult() 中图片已经保存”..onActivityResult 是您获得原始大小的捕获图片/位图的唯一步骤..您可以假设喜欢它的(onActivityResult)下一步单击设备相机中的捕获按钮..然后你可以做任何你想做的事情。我的回答非常直接和完美。它只是通过正确检查请求和结果代码来调整从相机收集的位图的大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2012-12-14
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多