2014-06-05 2 views
0

갤러리에서 이미지를 선택하고 imageview에 표시하는 시나리오가있는 안드로이드 앱을 만들고 있습니다. 자르기 시점에 이미지를 확대하고 싶습니다. 이미지를 확대/축소하는 데 TouchImageView 클래스를 사용하고 있습니다.이미지 확대/축소 및 이미지보기에 자른 이미지 표시

TouchImageViewImageView에만 적용하려면 잘 작동합니다. 그러나 작물 기능과 함께 사용하면 효과가 없습니다.

ImageView에 한 번에 자르기 + 확대/축소 기능을 적용해야합니까? 모든 유형의 도움을 주시면 감사하겠습니다. 아래는 내 시도입니다.

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     System.out.println(requestCode); 
     if (resultCode != RESULT_OK) { 

       return; 
      } 

      Bitmap bitmap; 

      switch (requestCode) { 

       case 0: 

        mImageCaptureUri = data.getData(); 
        doCrop(); 

        break; 
       case 1: 

        mImageCaptureUri = data.getData(); 
        doCrop(); 
        break; 
       case 2: 
        Bundle extras = data.getExtras(); 
        /** 
        * After cropping the image, get the bitmap of the cropped image and 
        * display it on imageview. 
        */ 
        if (extras != null) { 
          Bitmap photo = extras.getParcelable("data"); 
          img_v.setImageBitmap(photo); 

          //myBitmap = getCircleImage(photo); 

          //String image_base64 = postimage(); 

         // new PostCoverTask().execute(userid, image_base64); 
        } 

        File f = new File(mImageCaptureUri.getPath()); 
        /** 
        * Delete the temporary image 
        */ 
        if (f.exists()) 
          f.delete(); 
        break; 
      } 
      super.onActivityResult(requestCode, resultCode, data); 


    } 

    private void doCrop() 
    { 
     final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); 
     /** 
     * Open image crop app by starting an intent 
     * ‘com.android.camera.action.CROP‘. 
     */ 
     Intent intent = new Intent("com.android.camera.action.CROP"); 
     intent.setDataAndType(mImageCaptureUri,"image/*"); 

     /** 
     * Check if there is image cropper app installed. 
     */ 
     List<ResolveInfo> list = getPackageManager().queryIntentActivities(
       intent, 0); 

     int size = list.size(); 


     /** 
     * If there is no image cropper app, display warning message 
     */ 
     if (size == 0) 
     { 
      Toast.makeText(this, "Can not find image crop app", 
        Toast.LENGTH_SHORT).show(); 

      return; 
     } 
     else 
     { 
      size=1; 
      /** 
      * Specify the image path, crop dimension and scale 
      */ 
      // intent.setData(mImageCaptureUri); 
      intent.putExtra("crop", "true"); 
      intent.putExtra("outputX", 256); 
      intent.putExtra("outputY", 256); 
      intent.putExtra("aspectX",3); 
      intent.putExtra("aspectY", 1); 
      intent.putExtra("scale", true); 
      intent.putExtra("scaleUpIfNeeded", true); 
      intent.putExtra("return-data", true); 
      System.out.println("Put image in Extra"); 
      /** 
      * There is posibility when more than one image cropper app exist, 
      * so we have to check for it first. If there is only one app, open 
      * then app. 
      */ 

      if (size == 1) { 
       Intent i = new Intent(intent); 
       ResolveInfo res=null ; 
       for (int i1=0;i1<list.size();i1++) 
       { 
        if(list.get(i1)!=null) 
        { 
         res = list.get(i1); 
         break; 
        } 
       } 
       i.setComponent(new ComponentName(res.activityInfo.packageName, 
         res.activityInfo.name)); 
       System.out.println("size is equal to "+size); 
       startActivityForResult(i, 2); 
      } 
      else 
      { 
       System.out.println("size is equal to "+size); 
       /** 
       * If there are several app exist, create a custom chooser to 
       * let user selects the app. 
       */ 
       for (ResolveInfo res : list) 
       { 
        final CropOption co = new CropOption(); 

        co.title = getPackageManager().getApplicationLabel(
          res.activityInfo.applicationInfo); 
        co.icon = getPackageManager().getApplicationIcon(
          res.activityInfo.applicationInfo); 
        co.appIntent = new Intent(intent); 

        co.appIntent 
        .setComponent(new ComponentName(
          res.activityInfo.packageName, 
          res.activityInfo.name)); 

        cropOptions.add(co); 
       } 

       CropOptionAdapter adapter = new CropOptionAdapter(
         getApplicationContext(), cropOptions); 

       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("Choose Crop App"); 
       builder.setAdapter(adapter, 
         new DialogInterface.OnClickListener() 
       { 


        public void onClick(DialogInterface dialog, int item) { 
         System.out.println("option "+cropOptions.get(item)); 
         //startActivityForResult(cropOptions.get(item).appIntent, 
          // CROP_FROM_CAMERA); 
         System.out.println("builder.setAdapter"); 
        } 
       }); 

       builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 
        @Override 
        public void onCancel(DialogInterface dialog) { 

         if (mImageCaptureUri != null) { 
          getContentResolver().delete(mImageCaptureUri, null, 
            null); 
          mImageCaptureUri = null; 

         } 
         System.out.println("Click on cancel"); 
        } 
       }); 

       AlertDialog alert = builder.create(); 

       alert.show(); 
       System.out.println("alert"); 
      } 
     } 
    } 
+0

당신이 할 수있다, 당신은 "어쩌려 구't 작업"에 의해 무엇을 의미합니까 오류 메시지를 지정할 수 있을까요? –

+0

아니 위 코드가 정확한지 확인하십시오. 그러나 그것은 단지 나를 위해 이미지를 자릅니다. 나는 자르기 동안 나는 줌인 후 이미지의 특정 부분을 자르거나 자른다. – user3710617

답변

1

나는 Github의 cropimage 라이브러리를 사용 중입니다. 그것은 귀하의 요구 사항에 잘 맞습니다. 이것은 내 프로젝트에서이 라이브러리를 사용하는 방법입니다. 갤러리 또는 카메라에서

<activity android:name="eu.janmuller.android.simplecropimage.CropImage" /> 

이미지 선택 및 호출이 기능 : 매니페스트 파일에 다음 행을 추가

public void runCropImage(String path) { 
    Intent intent = new Intent(this, CropImage.class); 
    intent.putExtra(CropImage.IMAGE_PATH, path); 
    intent.putExtra(CropImage.SCALE, true); 
    intent.putExtra(CropImage.ASPECT_X, 2);//change ration here via intent 
    intent.putExtra(CropImage.ASPECT_Y, 2); 
    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);//final static int 1 
} 

을 그리고 당신의 onActivityResult를에 :

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    // gallery and camera ommitted 
    case REQUEST_CODE_CROP_IMAGE: 
     String path = data.getStringExtra(CropImage.IMAGE_PATH); 
     // if nothing received 
     if (path == null) { 
      return; 
     } 
     // cropped bitmap 
     Bitmap bitmap = BitmapFactory.decodeFile(path); 
     imageView.setImageBitmap(bitmap); 
     break; 
    default: 
     break; 
    } 
} 
+0

오른쪽하지만 자르기 도중 이미지 자르기 + 확대/축소를 원합니다 – user3710617

+0

확대/축소가 가능하도록 라이브러리를 사용해보십시오. 시도해 보았습니다. –

+0

내 활동 ///////////////////////////////////// ///////////////////// – user3710617

0

난 몰라 이것이 귀하의 문제를 해결할 수 있는지 확인하십시오. 내 프로젝트에 Camera 또는 Gallery에서 이미지를 선택하는 기능을 추가 한 다음 비슷한 기능을 이미지와 함께 고정 된 자르기 창에 표시하는 것과 비슷한 기능을 만들었습니다. Photoview와 Cropper 도구 결합. Github에서 코드를 공유했습니다. 필요에 맞게 코드를 수정할 수 있습니다. 프로젝트에 apk 파일을 추가했습니다. 에뮬레이터가 카메라를 제대로 처리하지 못하기 때문에 카메라 테스트에 실제 장치를 사용하십시오. 여기 내 프로젝트에 대한 링크가 있습니다.

https://github.com/ozeetee/AndroidImageZoomCrop

관련 문제