【问题标题】:Captured image displays blur捕获的图像显示模糊
【发布时间】:2019-06-28 04:32:20
【问题描述】:

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

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

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

【问题讨论】:

标签: android camera android-camera-intent photoeditorsdk


【解决方案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;
}

现在,您可以像这样调用捕获 Intent:

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);
}

【讨论】:

    【解决方案2】:

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

    thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    

    【讨论】:

      【解决方案3】:

      您可以使用所需的高度和宽度高值缩放位图以获得更多清晰度。

      首先你必须从你的意图数据 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多