位图图像调整大小

位图图像调整大小

问题描述:

我使用Intent单击图像并启动Android相机时出现问题。我通过意向数据得到的图像携带调整大小的位图图像的信息。也许我有一个错误的理解,但请建议我能做些什么来纠正它。该ImageView的显示我点击了相同的图像,而是一个非常blurrred一个位图图像调整大小

这里是底层代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
InputStream stream= null; 
     super.onActivityResult(requestCode, resultCode, data); 
     if(this.requestCode == requestCode && resultCode == RESULT_OK){ 
      try{ 
       //stream= getContentResolver().openInputStream(data.getData()); 
       Bundle extras= data.getExtras(); 
       Bitmap bitmap = (Bitmap) extras.get("data"); 
       imageHolder.setImageBitmap(bitmap); 
      } 
      catch (Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
    } 
+0

使用bitmap.createScaledBitmap(); –

+0

为什么不使用uri而不是位图? –

 try{ 
      //stream= getContentResolver().openInputStream(data.getData()); 
      Bundle extras= data.getExtras(); 
      Bitmap bitmap = (Bitmap) extras.get("data"); 
      //set value Width=200, Height=200 
      Bitmap resizedimg = Bitmap.createScaledBitmap(bitmap, Width, Height, true); 

      imageHolder.setImageBitmap(resizedimg); 
     } 
     catch (Exception ex){ 
      ex.printStackTrace(); 
     } 
+0

即使使用bitmap.createScaledBitmap后,图像仍然模糊不清 –

也许你应该通过一个URI来相机,并从中获得图像时ActivityResult代替位图的

试试这个:

public class MainActivity extends Activity { 

    static int CAMERA_TAKE_PIC =1; 
    Uri outPutfileUri; 
    ImageView mImageView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mImageView = (ImageView) findViewById(R.id.image); 
    } 


    public void CameraClick(View v) { 

     Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File file = new File(Environment.getExternalStorageDirectory(), 
       "Photo.jpg"); 
     outPutfileUri = Uri.fromFile(file); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri); 
     startActivityForResult(intent, CAMERA_TAKE_PIC); 
    } 

    Bitmap bitmap = null; 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode,Intent data) 
    { 
     if (requestCode == CAMERA_TAKE_PIC && resultCode==RESULT_OK) { 

      String uri = outPutfileUri.toString(); 
      Log.e("uri-:", uri); 
      Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show(); 

      //Bitmap myBitmap = BitmapFactory.decodeFile(uri); 
      // mImageView.setImageURI(Uri.parse(uri)); OR drawable make image strechable so try bleow also 

      try { 
       bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri); 
       Drawable d = new BitmapDrawable(getResources(), bitmap); 
       mImageView.setImageDrawable(d); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


     } 
} 

} 

我认为形象是模糊的,因为意图数据仅包含图像的thumbnail。您可以将图像保存在内部/外部存储上,然后获取它。

图像可能模糊,因为意图数据仅包含图像的缩略图。您必须将图像保存在内部/外部存储器中,然后获取它。您的问题应该是如何从相机获取全尺寸图像。 阅读本文档https://developer.android.com/training/camera/photobasics.html

这是怎么了应该做

Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
File dir= 
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 

output=new File(dir, "CameraContentDemo.jpeg"); 
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output)); 

startActivityForResult(i, CONTENT_REQUEST); 

Refrence可以在这个环节被发现。

https://*.com/a/32121829/3111083