Android 从摄影机到Imageview的图像分辨率
我想拍张照片,并将图像添加到活动中。我有一个奇怪的像素化,起初我以为这是我用的斑点,但它似乎与从相机捕获后的图像有关 我启动相机拍照Android 从摄影机到Imageview的图像分辨率,android,android-imageview,pixels,Android,Android Imageview,Pixels,我想拍张照片,并将图像添加到活动中。我有一个奇怪的像素化,起初我以为这是我用的斑点,但它似乎与从相机捕获后的图像有关 我启动相机拍照 Camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent cameraIntent = new Intent(android.provide
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,我不知道我做错了什么
我上传了一个小视频,以查看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);
}
}
我将向您展示我通过以下操作使用的确切代码 我调用DispatchTakePictureContent类
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);
}
}
}
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));
}
}
以及createImageFile类
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类
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);
}
}
}
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));
}
}
它将在gallery文件夹中创建一个图像文件,但不会显示在gallery应用程序本身上
我希望它能帮助一些人,但我仍然相信有更好的方法来做我想做的事情,但肯定会成功。我尝试了你的方法,但当我拍摄照片并单击“验证”图标时,它就停在那里了。当我尝试使用文件资源管理器查找时,并没有为此生成文件--编辑我在api19上也试过了,但没用(我在吃棒棒糖)