在保存之前调整从相机拍摄的图像的大小

时间:2015-11-18 08:45:45

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

我正在使用内置相机应用程序拍摄照片,并获得图像分辨率(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();

    }

2 个答案:

答案 0 :(得分:0)

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

yum

答案 1 :(得分:-1)

取自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
    }  
}