2017-03-04 3 views
0

갤러리에서 이미지를 선택하고 이미지를 자르기 위해 안드로이드에 코드를 구현했지만 자른 이미지는 매우 흐리게 보이며 자른 이미지를 그대로 표시하는 방법안드로이드에서 이미지를 선택하고 자르면 화질이 바뀝니다. (흐림)

ImageSelecter.java

public class ImageSelecter extends Activity { 

private final int GALLERY_ACTIVITY_CODE=200; 
private final int RESULT_CROP = 400; 
String picturePath; 
ImageView image_capture1; 
Button btn_choose; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn_choose=(Button)findViewById(R.id.choose_btn); 
    image_capture1=(ImageView)findViewById(R.id.img); 
    btn_choose.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //Start Activity To Select Image From Gallery 
      Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class); 
      startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE); 
      } 
     }); 

     } 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == GALLERY_ACTIVITY_CODE) { 
     if(resultCode == Activity.RESULT_OK){ 
      picturePath = data.getStringExtra("picturePath"); 
      //perform Crop on the Image Selected from Gallery 
      performCrop(picturePath); 
     } 
    } 

    if (requestCode == RESULT_CROP) { 
     if(resultCode == Activity.RESULT_OK){ 
      Bundle extras = data.getExtras(); 
      Bitmap selectedBitmap = extras.getParcelable("data"); 

      // Set The Bitmap Data To ImageView 
      image_capture1.setImageBitmap(selectedBitmap); 
       image_capture1.setScaleType(ImageView.ScaleType.FIT_XY); 
     } 
    } 
} 

private void performCrop(String picUri) { 
    try { 
     //Start Crop Activity 

     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     File f = new File(picUri); 
     Uri contentUri = Uri.fromFile(f); 

     cropIntent.setDataAndType(contentUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 1); 
     cropIntent.putExtra("aspectY", 1); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", 280); 
     cropIntent.putExtra("outputY", 280); 

     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, RESULT_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     // display an error message 
     String errorMessage = "your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 
} 

다음과 같이 갤러리 클래스는

GalleryU입니다 표시하는 클래스 : 여기

내 코드입니다 til.java

public class GalleryUtil extends Activity { 
    private final static int RESULT_SELECT_IMAGE = 100; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    private static final String TAG = "GalleryUtil"; 

    String mCurrentPhotoPath; 
    File photoFile = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try{ 
     //Pick Image From Gallery 
     Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(i, RESULT_SELECT_IMAGE); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode){ 
     case RESULT_SELECT_IMAGE: 

      if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { 
       try{ 
        Uri selectedImage = data.getData(); 
        String[] filePathColumn = {MediaStore.Images.Media.DATA }; 
        Cursor cursor = getContentResolver().query(selectedImage, 
          filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String picturePath = cursor.getString(columnIndex); 
        cursor.close(); 

        //return Image Path to the Main Activity 
        Intent returnFromGalleryIntent = new Intent(); 
        returnFromGalleryIntent.putExtra("picturePath",picturePath); 
        setResult(RESULT_OK,returnFromGalleryIntent); 
        finish(); 
       }catch(Exception e){ 
        e.printStackTrace(); 
        Intent returnFromGalleryIntent = new Intent(); 
        setResult(RESULT_CANCELED, returnFromGalleryIntent); 
        finish(); 
       } 
      }else{ 
       Log.i(TAG,"RESULT_CANCELED"); 
       Intent returnFromGalleryIntent = new Intent(); 
       setResult(RESULT_CANCELED, returnFromGalleryIntent); 
       finish(); 
      } 
      break; 
    } 
} 
    } 

자른 이미지를 그대로 표시하는 솔루션을 제공하십시오.

답변

0

ImageView.ScaleType.FIT_XY을 ImageView.ScaleType.CENTER_INSIDE로 변경하십시오.

또는 FIT_CENTER/CENTER_CROP/CENTER과 같은 다른 적절한 ScaleType.