从相机到 Imageview 的图像分辨率
Resolution of image from camera to Imageview
我想拍照并将图像添加到 activity。我有一个奇怪的像素化,起初我认为这是我使用的斑点的问题,但它似乎与从相机捕获后的图像有关。
我启动相机拍照
Camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
那我想在imageview上设置图片
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
int width = photo.getWidth();
int height = photo.getHeight();
Toast.makeText(getApplicationContext(),"Width:"+width+" / Height:"+height,Toast.LENGTH_SHORT).show();
Screen.setImageBitmap(photo);
}
}
在我拍摄图像后,有一个预览来验证我想要的图像,图片像素化,我得到的位图分辨率是 150x205,我不知道我做错了什么。
我上传了一个小视频,看看实际分辨率http://youtu.be/s5Cu3QYSDto
在 android 中,您从相机获取的默认数据是低分辨率缩略图。
因此,在您调用 CameraIntent 之前,请根据该文件路径创建一个文件和 uri,如此处所示。
filename = Environment.getExternalStorageDirectory().getPath() + "/folder/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));
// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult (cameraIntent, CAMERA_REQUEST);
现在,您有了可以在 onAcityvityResult 方法中使用的文件路径,如下所示,您还可以从 uri 中获取位图。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
ImageView img = (ImageView) findViewById(R.id.image);
img.setImageURI(imageUri);
}
}
我将按照 developer page.
向您展示我使用的确切代码
我调用 dispatchTakePictureIntent class.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.d("Error creating image file","CAM");
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
uriphoto = Uri.fromFile(photoFile);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
和 createImageFile class
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
和 onActivityResult class.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Screen.setImageURI(uriphoto);
}
else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Screen.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
它将在图库文件夹中创建一个图像文件,但不会显示在图库应用程序本身上。
我希望它对某些人有所帮助,但我仍然确信有更好的方法来做我想做的事情,但肯定它有效。
我想拍照并将图像添加到 activity。我有一个奇怪的像素化,起初我认为这是我使用的斑点的问题,但它似乎与从相机捕获后的图像有关。
我启动相机拍照
Camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
那我想在imageview上设置图片
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
int width = photo.getWidth();
int height = photo.getHeight();
Toast.makeText(getApplicationContext(),"Width:"+width+" / Height:"+height,Toast.LENGTH_SHORT).show();
Screen.setImageBitmap(photo);
}
}
在我拍摄图像后,有一个预览来验证我想要的图像,图片像素化,我得到的位图分辨率是 150x205,我不知道我做错了什么。
我上传了一个小视频,看看实际分辨率http://youtu.be/s5Cu3QYSDto
在 android 中,您从相机获取的默认数据是低分辨率缩略图。
因此,在您调用 CameraIntent 之前,请根据该文件路径创建一个文件和 uri,如此处所示。
filename = Environment.getExternalStorageDirectory().getPath() + "/folder/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));
// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult (cameraIntent, CAMERA_REQUEST);
现在,您有了可以在 onAcityvityResult 方法中使用的文件路径,如下所示,您还可以从 uri 中获取位图。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
ImageView img = (ImageView) findViewById(R.id.image);
img.setImageURI(imageUri);
}
}
我将按照 developer page.
向您展示我使用的确切代码我调用 dispatchTakePictureIntent class.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.d("Error creating image file","CAM");
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
uriphoto = Uri.fromFile(photoFile);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
和 createImageFile class
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
和 onActivityResult class.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Screen.setImageURI(uriphoto);
}
else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Screen.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
它将在图库文件夹中创建一个图像文件,但不会显示在图库应用程序本身上。
我希望它对某些人有所帮助,但我仍然确信有更好的方法来做我想做的事情,但肯定它有效。