【问题标题】:When I capture image from camera it lost the quality of image? What to do?当我从相机捕捉图像时,它失去了图像质量?该怎么办?
【发布时间】:2016-05-13 05:37:51
【问题描述】:

我点击图片的代码是:

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

活动结果代码:

if (requestCode == 300) { 
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, 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();
    }

    if(number.equalsIgnoreCase("1"))
    {
        imageViewOneNumber.setImageBitmap(thumbnail);
            image1="";
            image1= getEncoded64ImageStringFromBitmap(thumbnail);
    }
    else
    {
        RelativeLayoutImage2.setVisibility(View.GONE);
        FrameImage2.setVisibility(View.VISIBLE);
        imageViewTwoNumber.setImageBitmap(thumbnail);
        image2="";
        image2= getEncoded64ImageStringFromBitmap(thumbnail);
    }
}

相机拍摄演示图片:

请帮我解决这个问题。当我单击相机中的照片时,它会减小图像的大小。

【问题讨论】:

  • 您需要在启动意图时设置要保存的图像路径。如果你不设置路径,API 只返回一个缩略图版本,因此会模糊
  • 如何设置路径? @Shubhank

标签: android camera imageview


【解决方案1】:

来自docs

如果您提供,Android 相机应用程序会保存一张全尺寸照片 要保存到的文件。您必须提供完全限定的文件名 相机应用应保存照片的位置。

那里的示例代码

  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) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }

String mCurrentPhotoPath;

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

【讨论】:

    【解决方案2】:

    开始意图

     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    startActivityForResult(intent, 300);
    

    onActivityResult(){..}

        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {  
        //Check that request code matches ours:
        if (requestCode == 300) 
        {
            //Get our saved file into a bitmap object:
           File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
           Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
        }
    }
    

    这里是decodeSampledBitmapFromFile()

        public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
    { // BEST QUALITY MATCH
    
        //First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
    
        // Calculate inSampleSize, Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;
    
        if (height > reqHeight) 
        {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }
        int expectedWidth = width / inSampleSize;
    
        if (expectedWidth > reqWidth) 
        {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    
        options.inSampleSize = inSampleSize;
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
    
        return BitmapFactory.decodeFile(path, options);
        }
    

    【讨论】:

    • 完美...得到输出...@Devendra
    【解决方案3】:

    这样实现:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File image = createImage(this);
                    Uri uri = Uri.parse("file://" + image.getAbsolutePath());
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(intent, CAMERA_REQUEST);
    
    public File createImage(Context context) throws IOException {
            File dir = new File(Environment.getExternalStorageDirectory() + "/" + context.getString(R.string.company_name) + "/Images");
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    throw new IOException("Something wrong happened at" + dir);
                }
            }
            String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault()).format(new Date());
            String imageName = context.getString(R.string.app_name) + "_" + timeStamp + ".jpg";
            return new File(dir.getPath() + File.separator + imageName);
        }
    

    最后在onActivityResult() 你可以得到你的图片:

    if (requestCode == CAMERA_REQUEST) {
                //Here you can load image by Uri
            }
    

    【讨论】:

      【解决方案4】:

      使用此代码可能会对您有所帮助

      Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                          String path=Environment.getExternalStorageDirectory()+File.separator+Constants.APP_FOLDER_NAME+File.separator+Constants.ATTACHMENTS_FOLDER_NAME;
                          File mediapath=new File(path);
                          if(!mediapath.exists())
                          {
                              mediapath.mkdirs();
                          }
                          captured_image_uri=null;
                          captured_image_uri=Uri.fromFile(new File(mediapath.getPath(),"Image"+System.currentTimeMillis()+".jpg"));
                          intent.putExtra(MediaStore.EXTRA_OUTPUT,captured_image_uri);
                          startActivityForResult(intent, Constants.PICK_FILE_FROM_CAMERA);
      

      onActivityResult 写这段代码

      if(requestCode==Constants.PICK_FILE_FROM_CAMERA&&resultCode==getActivity().RESULT_OK)
                  {
                      try
                      {
                          if(captured_image_uri!=null) {
                              ExifInterface exifInterface = new ExifInterface(captured_image_uri.getPath());
                              int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                              Matrix matrix = new Matrix();
                              switch (orientation) {
                                  case ExifInterface.ORIENTATION_ROTATE_90: {
                                      matrix.postRotate(90);
                                      break;
                                  }
                                  case ExifInterface.ORIENTATION_ROTATE_180: {
                                      matrix.postRotate(180);
                                      break;
                                  }
                                  case ExifInterface.ORIENTATION_ROTATE_270: {
                                      matrix.postRotate(270);
                                      break;
                                  }
                              }
                              FileInputStream fis = new FileInputStream(captured_image_uri.getPath());
                              Bitmap bmp = BitmapFactory.decodeStream(fis);
                              Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                              FileOutputStream fos = new FileOutputStream(captured_image_uri.getPath());
                              rotated.compress(Bitmap.CompressFormat.JPEG, 85, fos);
                              uploadFileToServer(captured_image_uri.getPath());
                          }
                      }catch (Exception e)
                      {
      
                                     e.printStackTrace();
                      }
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-19
        • 2014-03-23
        • 1970-01-01
        • 2020-03-17
        • 2012-06-16
        • 1970-01-01
        • 2012-04-05
        • 2013-04-18
        相关资源
        最近更新 更多