4

I want image being shown in imageview to be selected with particular portion(and only selected portion needs to highlighted and other portion to be semi transparent) and that portion can also be resized as needed or done done by user on touch event.

Now, the selected portion of image is needed to be croped and then show and save that cropped image.

EDIT:

I used Intent to open the image and crop it using intent.putExtra("crop","true");

But while passing intent I want to open image whose URI is already known instead of opening the whole album of image gallery.

Can anyone suggest, how can I open particular URI through intent passing for opening image. Thanks in advance.

Nikki
  • 3,314
  • 13
  • 36
  • 59
  • All mentioned image operations you may achieve using Canvas and Bitmap class. See for example http://stackoverflow.com/questions/4688306/how-can-i-make-image-opaque-to-some-level. To make some parts of the image semitransparent use XFer methods from android.graphics. – Zelimir Feb 16 '11 at 13:42
  • @Zelimir: Thanks but I have already seen that question but it doesn't contain about cropping the image in imageview. Can you please let me know about cropping the image – Nikki Feb 17 '11 at 06:11
  • 1
    @Nikki: So, what you need is to take some image, crop part of it (and save it), and make the rest semi transparent? – Zelimir Feb 17 '11 at 06:44
  • @Zelimir: yes......right, i need to do this, as you explained right now – Nikki Feb 17 '11 at 07:14
  • @Nikki: Easiest way is to use Bitmap android.graphics.Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) and create new one from that. For partial transparency use XFer mode functions. – Zelimir Feb 17 '11 at 08:02
  • @Zelimir: can you please explain with example as i am not able to understand it completely. – Nikki Feb 17 '11 at 08:12
  • Everything is explained in the function description. You have source Bitmap and new Bitmap is created using just rectangular part from it. Currently I am busy, but if you do not make it today I will make some example. – Zelimir Feb 17 '11 at 08:37
  • @Zelimir: ok! thanks......I will try to understand the description but if you can, then make example also. – Nikki Feb 17 '11 at 10:14
  • @Zelimir: Can you please provide me some example. I am unable to do as I have to select portion of image dynamically by selecting the portion dynamically and then cropping that part of image and save it. Can u plz help me out of this problem. – Nikki Feb 18 '11 at 12:27
  • @Nikki: But answer from Josh explains you how to take smaller picture as a part from the bigger. What is that you do not know? How to save Bitmap to file? How to create bigger picture that has missing the cropped part? – Zelimir Feb 18 '11 at 14:09
  • @Zelimir: As the answer provided by josh, only crops the particular part from the screen but how to select that part dynamically is not explained and moreover, I also need to move that selected area to represent another portion as it does at the time of actual cropping the image. – Nikki Feb 21 '11 at 04:09
  • @Nikki: Added my answer, hope it is what you need. – Zelimir Feb 21 '11 at 07:08
  • @Zelimir: As answer provided by you, MODE.CLEAR not working with xfermode. Can you please let me know why is it so? – Nikki Feb 21 '11 at 07:28
  • @Nikki: What do you mean not working? I get top left part of image cleared (cropped). This is the code I checked before putting into your answer. Another option is that maybe you are working with immutable bitmaps. Do you get error message? – Zelimir Feb 21 '11 at 07:47
  • @Zelimir: Sorry........but now that's code working fine now. But it is not cropping the image and nor showing rectangular box around the image to select the portion of the image. – Nikki Feb 21 '11 at 08:01
  • @Nikki: Well, you need to take care about rectangular box and other fancy GUI features. You cannot expect to get full solution from me. I gave you just directions (and moret than that) how to solve the issue, the rest you have to develop yourself. – Zelimir Feb 21 '11 at 08:07
  • @Zelimir: But it is not cropping the image also, just bringing some transparency to the image – Nikki Feb 21 '11 at 08:10
  • @Zelimir: I would like to have same as it have in cropping feature when we pass intent and open gallery and then we crop the image there. I would like to have same feature here with imageview – Nikki Feb 21 '11 at 08:19
  • @Nikki: Cropping image part you already got. I just gave yo you idea how to crop it. If you want the same bahaviour, there is a system Intent for image crop, like one from gallery. Or you want to do it yourself? – Zelimir Feb 21 '11 at 08:47
  • @Zelimir: I know about the system Intent but I also want to rotate the image on same activity. That's why I want same behaviour as it does with Intent but also for rotation with the same. If not possible then to do it by myself. – Nikki Feb 21 '11 at 08:59
  • @Nikki: To get it work/look the same will request for more work. Good luck with that. – Zelimir Feb 21 '11 at 09:18
  • @Zelimir: Thanks a lot for your help you tried to give me. Can you just answer one question wheather I can directly open the image by giving its uri path by passing intent instead of firstly opening gallery and choosing from that gallery. – Nikki Feb 21 '11 at 09:35
  • @Nikki: Yes you can. I updated my answer for that. – Zelimir Feb 21 '11 at 10:08
  • @Zelimir: Thanks......but I would like to apply this feature with i.putExtra("crop","true"); Can I do this? As what provided by you doesn't work for cropping the image directly without opening gallery. And I also can't see the image on opening this activity – Nikki Feb 21 '11 at 10:29
  • @Nikki: maybe you do not see picture because it is the new one you just created and not recognized with MediaScanner (supposing your Uri is correct). AFAIK system "crop" works on the picture taken from Camera or selected from Gallery, but not otherwise. – Zelimir Feb 21 '11 at 10:38
  • @Zelimir: yes, I would like to work with selected from gallery but the uri path must be given by me and not choosen from gallery at runtime. That's why I doesn't want to show the gallery while intent calling – Nikki Feb 21 '11 at 10:51
  • @Zelimir: Thank you ........so much for your help! I haven't tried yet but I think this could help me out of my problem. I am just going to try it, right now in my code. – Nikki Feb 24 '11 at 05:26

3 Answers3

3

Regarding the last part of your question, if you're on the very latest Gingerbread (2.3.3, API level 10), you can use BitmapRegionDecoder to crop an image.

It's useful because, until this API existed, you had to load the entire image into memory before you could crop. With 5mpix and 8mpix cameras this is usually impossible without subsampling (i.e. the cropped image loses lots of resolution).

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
2

UPDATE: Changed my answer after question was edited and more precisely described.

Issue you are facing with has long history, also at SO:

unable to find com.android.camera.CropImage activity in android

One answer has described what you need. Please note that you are using intent that is not part of the official SDK, and you may encounter different kind of issues. Issue I experienced was using crop immediatelly after image was taken by the camera. Also, it is not compatible through different Android versions, so if you get it working for 1.5 maybe it will not work for 2.3. Other useful links:

http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/569f36b5b28f2661?lnk=gst&q=Crop+image+intent#569f36b5b28f2661

http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/dcbe5aef29eddad6?lnk=gst&q=Crop+image+intent#dcbe5aef29eddad6

http://groups.google.com/group/android-developers/browse_thread/thread/d7b6a133c164aa17/184bf3b85da2ce58?lnk=gst&q=Crop+image+intent#184bf3b85da2ce58

Community
  • 1
  • 1
Zelimir
  • 11,008
  • 6
  • 50
  • 45
1

Check out my answer to this question. It doesn't deal with the touch-to-resize aspect of your question, but handles drawing parts of an image over the original image.

Bottom line is, you don't want to use an ImageView, since that's mainly for displaying a static image with various scaling properties. You're better off using a custom view with an overridden draw() method.

Community
  • 1
  • 1
Josh
  • 10,618
  • 2
  • 32
  • 36