2016-05-13 44 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(); 
       } 

      }