如何设置 android 相机拍摄的照片的大小和质量?
How to set the size and quality of a picture taken with the android camera?
我写了这段代码,用相机拍照然后将其转换为 jpeg 文件并将其作为 ParseFile 上传到 Parse.com。
问题是图片真的很小(153 x 204 像素)而且质量很差。
我需要图片质量至少为 5 MP 并将其裁剪为 300x300 像素。
这是我目前使用的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_picture);
img = (ImageView) findViewById(R.id.imgV);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
byte[] image_byte_array;
ParseObject post_object = new ParseObject("Gallery");
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image_byte_array = stream.toByteArray();
ParseFile picture_file = new ParseFile("Picture.jpg", image_byte_array);
picture_file.saveInBackground();
post_object.put("photo", picture_file);
post_object.saveInBackground();
}
提前致谢。
检查下面的link
http://developer.android.com/guide/topics/media/camera.html#intent-image
你必须添加一个 Uri
这是文件的 Uri 来存储照片
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
完整样本
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_picture);
img = (ImageView) findViewById(R.id.imgV);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
勾选this answer here.
A camera intent return a thumbnail by default, so you need to
fetch the full image.
Derek 的解决方案是正确的,但你必须改进它。
真正的好处在于获取此处发生的完整图像 -
thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
imgView.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);
完整路径将由 getRealPathFromURI()
给出。
我写了这段代码,用相机拍照然后将其转换为 jpeg 文件并将其作为 ParseFile 上传到 Parse.com。
问题是图片真的很小(153 x 204 像素)而且质量很差。
我需要图片质量至少为 5 MP 并将其裁剪为 300x300 像素。
这是我目前使用的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_picture);
img = (ImageView) findViewById(R.id.imgV);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
byte[] image_byte_array;
ParseObject post_object = new ParseObject("Gallery");
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
image_byte_array = stream.toByteArray();
ParseFile picture_file = new ParseFile("Picture.jpg", image_byte_array);
picture_file.saveInBackground();
post_object.put("photo", picture_file);
post_object.saveInBackground();
}
提前致谢。
检查下面的link
http://developer.android.com/guide/topics/media/camera.html#intent-image
你必须添加一个 Uri
这是文件的 Uri 来存储照片
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
完整样本
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_picture);
img = (ImageView) findViewById(R.id.imgV);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
勾选this answer here.
A camera intent return a thumbnail by default, so you need to fetch the full image.
Derek 的解决方案是正确的,但你必须改进它。
真正的好处在于获取此处发生的完整图像 -
thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
imgView.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);
完整路径将由 getRealPathFromURI()
给出。