Friday 4 January 2013

Crop an image when selected from gallery in android


                     I'm facing a problem with parsing a Uri of taken photo to crop activity. In my application, users can take a photo or select one from gallery and then crop it and upload it. Here I can’t crop whole image.
I use the below code:

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);            
startActivityForResult(intent, REQUEST_CODE_CROP);

             Now I got the solution to this crop functionality for take picture or from gallery. Following code I used its working if any other way is their please let me know.:


case REQ_CODE_PICK_IMAGE:
if (resultCode == RESULT_OK) {
  send.setVisibility(View.VISIBLE);
//             Display();
 gallery.setVisibility(View.INVISIBLE);                               
 img_gallery.setVisibility(View.INVISIBLE);
           if (imageReturnedIntent != null)
                               {
           File tempFile = getTempFile();
            String filePath = Environment.getExternalStorageDirectory()+ "/temporary_holder.jpg";
           System.out.println("path " + filePath);
           Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
           ImageView imageView = (ImageView) findViewById(R.id.bakgnd);
           int imgHeight = selectedImage.getHeight();
          int imgWidth = selectedImage.getWidth();
          int containerHeight = imageView.getHeight();
           int containerWidth = imageView.getWidth();
          boolean ch2cw = containerHeight > containerWidth;
         float h2w = (float) imgHeight / (float) imgWidth;
          float newContainerHeight, newContainerWidth;
         if (h2w > 1) {
        if (ch2cw) {
                           newContainerWidth = (float) containerWidth;
                           newContainerHeight = newContainerWidth * h2w;
                                 }
                     else {
                                   newContainerHeight = (float) containerHeight;
                                   newContainerWidth = newContainerHeight / h2w;
                              }
                        } 
                  else {
                        if (ch2cw) {
                              newContainerWidth = (float) containerWidth;
                               newContainerHeight = newContainerWidth / h2w;
             } else {
                           newContainerWidth = (float) containerHeight;
                           newContainerHeight = newContainerWidth * h2w;
                             }
                       }
            image = Bitmap.createScaledBitmap(selectedImage,
            (int) newContainerWidth, (int) newContainerHeight,
                           false);
             imageView.setBackgroundDrawable(new BitmapDrawable(image));
                     //                             img.setBackgroundDrawable(null);
            }

No comments:

Post a Comment