2012-09-05 53 views
1

我通过下面的代码拍摄照片并保存到SD卡,但它生成的图片质量如此之低,甚至100%的质量都很差。也许bitmap.compress是不正确的方式去(或位图在所有?)Android保存的图像质量差

继承人我的代码:

public class TakePhoto extends Activity { 

    ImageView iv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_take_photo); 


     iv = (ImageView) findViewById(R.id.imageView1); 

     Button b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, 0); 

      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 

     Bitmap bm = (Bitmap) data.getExtras().get("data"); 
     Random generator = new Random(); 
     String randFileName = String.valueOf (generator.nextInt(965) + 32); 
     String both = "/mnt/extSdCard/DirectEnquiries/"+ randFileName + ".jpg"; 
     File imageFile = new File(both); 

     writeBitmapToMemory(imageFile, bm); 
     iv.setImageBitmap(bm); 

    } 

    public void writeBitmapToMemory(File file, Bitmap bitmap) { 
     FileOutputStream fos; 

     try { 
      Log.e("Tom", "Starting take stream"); 
      fos = new FileOutputStream(file); 
      Log.e("Tom", "Got stream"); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      Log.e("Tom", "Saved Image"); 
      fos.close(); 

     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 


     } 
     catch (IOException e) { 
      e.printStackTrace(); 


     } 

    } 

} 
+1

为什么使用位图使用简单文件复制操作。 – user370305

+0

你介意阐述吗? – TMB87

+0

由于相机已经存储了高质量的图像.. – user370305

回答

1

请拨打下面的函数从摄像机捕获图像。

private final static String FOLDER_NAME = "YourAppName/Image/"; 
private Uri selectedImageUri = null; 

public void startCamera() 
    { 
     File photo = null; 
     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
     { 
      photo = new File(android.os.Environment.getExternalStorageDirectory(), FOLDER_NAME+File.separator+timeStamp+".png"); 
     } 
     else 
     { 
      photo = new File(getCacheDir(), FOLDER_NAME+File.separator+timeStamp+".png"); 
     }  
     if (photo != null) 
     { 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
      selectedImageUri = Uri.fromFile(photo); 
      startActivityForResult(intent, CAPTURE_IMAGE_CALLBACK); 
     } 
    } 

您可以在selectedImageUri变量中获取图像Uri。 (图像存储在SD卡中)

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     switch (requestCode) 
     { 
      case CAPTURE_IMAGE_CALLBACK: 

       break; 
      } 
    } 
+0

我原来,但仍然有同样的问题 – TMB87

+0

@TomBeech检查我更新的答案。 –

+0

谢谢你。我需要做回调还是可以将其设置为0? – TMB87

1

您是否尝试过在意图中设置质量?

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);     
startActivityForResult(intent, 0); 
+0

MediaStore.EXTRA_VIDEO_QUALITY不适用于我试过的设备,Kitkat,4.4.4 –