0

我正在尝试从相机捕获图像,但结果是模糊的。下面是我正在使用的代码。

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, CAMERA_REQUEST);

  Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
  mPhotoEditorView.getSource().setImageBitmap(thumbnail);
4

3 回答 3

1
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

以上方法仅提供缩略图。

要保存全尺寸照片,您应该遵循本教程

在您的清单中添加此权限。

<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);
}
于 2019-06-28T05:06:22.020 回答
0

尝试使用以下代码解决模糊问题

thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
于 2019-06-28T05:19:04.433 回答
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;
}
于 2019-06-28T06:12:55.757 回答