【发布时间】: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