2016-05-13 47 views
2

我了点击的图像上的代码是:当我从相机捕捉图像时,它失去了图像的质量?该怎么办?

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

图片: Image which i got

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

+1

您需要为启动意图时保存的图像设置路径。如果你不设置路径,API只返回缩略图版本,因此模糊 – Shubhank

+0

我如何设置路径? @Shubhank – Krupa

回答

3

docs

Android相机应用程序,如果你给它一个 文件保存到保存一个全尺寸的照片。您必须提供完全合格的文件名 ,相机应用程序应该保存该照片。在onActivityResult()

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

最后你可以得到你的形象:

示例代码从那里

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

ThanqqQQQQ ...工作。 – Krupa

1

这样实现它

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

开始意图

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

Perfact ...得到了输出... @Devendra – Krupa

+1

:)开心编码 –

1

使用此代码可能是它可以帮助你

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

      }