我正在尝试从相机捕获图像,但是图像变得模糊。下面是我正在使用的代码。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mPhotoEditorView.getSource().setImageBitmap(thumbnail);
答案 0 :(得分:1)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
以上方法仅提供缩略图。
要保存全尺寸照片,请遵循此tutorial。
在您的清单中添加此权限。
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
...
</manifest>
创建一个文件。在此,我们将保存图像:
String currentPhotoPath;
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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
现在,您可以像这样调用捕获意图:
static final int REQUEST_TAKE_PHOTO = 1;
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
...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
在清单文件中添加FileProvider
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
...
</application>
创建具有以下内容的 res / xml / file_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>
在这里,您已将完整尺寸的图像保存到创建的文件中。
奖励::在ImageView中使用图像之前,应始终缩放图像,这将帮助您优化应用程序的内存使用率。
private void setPic() {
// Get the dimensions of the View
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
imageView.setImageBitmap(bitmap);
}
答案 1 :(得分:0)
尝试使用以下代码解决模糊问题
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
答案 2 :(得分:0)
您可以使用所需的高度和宽度高值来缩放位图,以提高清晰度。
首先,您必须从意图数据uri获取路径。
private String getpath(Context context, Uri uri) {
String filePath = null;
try {
Cursor cursor = context.getContentResolver().query(uri, null, null,
null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
filePath = cursor.getString(dataColumn);
}
cursor.close();
}
} catch (IllegalStateException e) {
}
return filePath;
}
然后使用此代码获取图像位图
public static Bitmap scaleImage(String p_path, int p_reqHeight, int p_reqWidth) throws Throwable {
Bitmap m_bitMap = null;
File m_file = new File(p_path);
if (m_file.exists()) {
BitmapFactory.Options m_bitMapFactoryOptions = new BitmapFactory.Options();
m_bitMapFactoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
m_bitMapFactoryOptions.inSampleSize = calculateInSampleSize(m_bitMapFactoryOptions, p_reqHeight, p_reqWidth);
m_bitMapFactoryOptions.inJustDecodeBounds = false;
m_bitMap = BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
} else {
throw new Throwable(p_path + " not found or not a valid image");
}
return m_bitMap;
}