【问题标题】:Android capture image from camera as thumbnail but not as it should beAndroid从相机捕获图像作为缩略图,但不是应该的
【发布时间】:2016-09-02 03:54:11
【问题描述】:

我有一些从相机拍照的 android 代码。但它把图片作为缩略图,但我想把图片因为它是原始大小。这是我的代码:

private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        CommonResources.photoFinishBitmap = thumbnail;
        goForEditing();
        //ivImage.setImageBitmap(thumbnail);
    }

你能帮我如何获取原始图像而不是缩略图。

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    您需要为要保存的全尺寸照片提供文件名。如果您在 Android 6.0 或更高版本上运行,您还需要访问文件存储。

    此答案应提供将全尺寸图像保存到文件的代码示例:https://stackoverflow.com/a/20353771/5527154

    这是来自 Google 的文档:https://developer.android.com/training/camera/photobasics.html#TaskPath

    【讨论】:

      【解决方案2】:

      要拍照,首先我们需要在AndroidManifest.xml 中声明所需的权限。我们需要两个权限:

      • Camera - 打开相机应用程序。如果属性required 设置为true,如果您没有硬件摄像头,您将无法安装此应用。
      • WRITE_EXTERNAL_STORAGE - 创建新文件需要此权限,拍摄的照片将保存在该文件中。

      AndroidManifest.xml

      <uses-feature android:name="android.hardware.camera"
                android:required="true" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      

      从相机拍摄全尺寸照片的主要想法是,在我们打开相机应用程序并拍摄照片之前,我们需要为照片创建新文件。

      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) {
                  Log.e("DEBUG_TAG", "createFile", ex);
              }
              // Continue only if the File was successfully created
              if (photoFile != null) {
                  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                  startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
              }
          }
      }
      
      private File createImageFile() throws IOException {
          // Create an image file name
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
          String imageFileName = "JPEG_" + timeStamp + "_";
          File storageDir = getAlbumDir();
          File image = File.createTempFile(
                  imageFileName,  /* prefix */
                  ".jpg",         /* suffix */
                  storageDir      /* directory */
          );
      
          // Save a file: path for use with ACTION_VIEW intents
          mCurrentPhotoPath = image.getAbsolutePath();
          return image;
      }
      
      private File getAlbumDir() {
          File storageDir = null;
      
          if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
      
              storageDir = new File(Environment.getExternalStorageDirectory()
                      + "/dcim/"
                      + "MyRecipes");
      
              if (!storageDir.mkdirs()) {
                  if (!storageDir.exists()) {
                      Log.d("CameraSample", "failed to create directory");
                      return null;
                  }
              }
      
          } else {
              Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
          }
      
          return storageDir;
      }
      
      private void setPic() {
      
          /* There isn't enough memory to open up more than a couple camera photos */
          /* So pre-scale the target bitmap into which the file is decoded */
      
          /* Get the size of the ImageView */
          int targetW = recipeImage.getWidth();
          int targetH = recipeImage.getHeight();
      
          /* Get the size of the image */
          BitmapFactory.Options bmOptions = new BitmapFactory.Options();
          bmOptions.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
          int photoW = bmOptions.outWidth;
          int photoH = bmOptions.outHeight;
      
          /* Figure out which way needs to be reduced less */
          int scaleFactor = 2;
          if ((targetW > 0) && (targetH > 0)) {
              scaleFactor = Math.max(photoW / targetW, photoH / targetH);
          }
      
          /* Set bitmap options to scale the image decode target */
          bmOptions.inJustDecodeBounds = false;
          bmOptions.inSampleSize = scaleFactor;
          bmOptions.inPurgeable = true;
      
          Matrix matrix = new Matrix();
          matrix.postRotate(getRotation());
      
          /* Decode the JPEG file into a Bitmap */
          Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
          bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
      
          /* Associate the Bitmap to the ImageView */
          recipeImage.setImageBitmap(bitmap);
      }
      
      private float getRotation() {
          try {
              ExifInterface ei = new ExifInterface(mCurrentPhotoPath);
              int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
      
              switch (orientation) {
                  case ExifInterface.ORIENTATION_ROTATE_90:
                      return 90f;
                  case ExifInterface.ORIENTATION_ROTATE_180:
                      return 180f;
                  case ExifInterface.ORIENTATION_ROTATE_270:
                      return 270f;
                  default:
                      return 0f;
              }
          } catch (Exception e) {
              Log.e("Add Recipe", "getRotation", e);
              return 0f;
          }
      }
      
      private void galleryAddPic() {
          Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
          File f = new File(mCurrentPhotoPath);
          Uri contentUri = Uri.fromFile(f);
          mediaScanIntent.setData(contentUri);
          sendBroadcast(mediaScanIntent);
      }
      
      private void handleBigCameraPhoto() {
      
          if (mCurrentPhotoPath != null) {
              setPic();
              galleryAddPic();
          }
      }
      
      
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
              handleBigCameraPhoto();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-11
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 2015-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多