Automatically crop image boundaries - java

I'm looking for an automatic way to grab a piece of a bitmap and extract a certain part of it.
Let me explain:
If I have this image:
http://imgur.com/B9U9E
It has a big white border. (see link for better white result) I'm looking for a way to only grab the dialog above. So no white borders around the dialog. Is there a way to do this by code or with a library?
You should know that the image could have any form and positioned anywhere on the white dialog.
So a user draws something on the white panel and i need the program to automatically make a rectangle about where the users drew on the canvas and save that bitmap where the user drew on the canvas (Everything in between that rectangle).

Pseudocode
Define the background color.
Scan from the left, right, bottom, top and store the locations of the transitions from background to drawing.
The rectangle defined by (left, bottom) and (right, top) defines the cropping area
For a Java code example, please see: How to auto crop an image white border in Java?

Look into Bitmap.createBitmap.

Related

How to draw border aound image

I have a ImageView with image, how can I draw border around image. The main problem is that image not a rectangle or circle and not cover full View. For example I want make something like this:
This is not so trivial. But I think if you use these steps, you should be able to pull this off:
1) Extract Bitmap from ImageView (or instead just take it directly from the resource you are using).
2) Iterate over all the pixels. If one of the neighbor pixels is not empty (transparent/white) and current pixel is empty then set pixel to red (do this only after you finished the iteration).
3) Set bitmap back to ImageView.

Displaying a part of an image in libGDX - looks stretched

I'm trying to build an progress bar in libGDX, for that I have one full horizontal image and in two lines I trying to display 2 different widths of that image:
imageFull:
imageFull.draw(batch,10,80,600,50);
imageFull.draw(batch,10,20,100,50);
the result is:
Its looks like when the width is 'small' its stretched and looks bad.
Why I can't display only part of the image without destroying the left side of the image?
Any ideas how to fix it?
That is normal behavior. If you stretch a image without keeping it's aspect ratio it will deform, it does not know the stretchable part by itself.
9-patch will help you here but you cannot simply draw the sprite as you are doing now (maybe with SpriteDrawable though?).
If I where you I would use the Scene2D Actor named ProgressBar. Feed that a 9-patch image, then it should stretch correctly. Or just use a Image if you want to control it yourself, and feed this image a ninepatch.
A quick way to create a ninepatch is to specify it's stretching regions yourself by hardcoding it.
texture = new NinePatch(yourTexture, A, B, C, D)
Where ABCD corresponds to the following image:
Now create a Scene2D Image with that ninepatch and it should stretch properly.
If you have a this texture already in a Atlas you can also supply the line split:a,b,c,d to the image data in the .atlas file and Scene2D will automatically pick it up as a ninepatch.
If you don't want to use Scene2D and/or ninepatch (but I recommend you to use it) you have to code the behavior yourself. Or cut the texture up yourself and stick the caps on the left and right side of your rectangle. But Scene2D is invented for this and a ton more GUI functionality.

Java - Getting screen position of buffered image

I'm working with some code where I have a few buffered images in a scrollbar. When I click one of these images, I want to draw a rectangle around the image to indicate that the image is selected.
My issue is that because my panel is scrollable, the image position set when drawing is not necessarily the actual screen position. So when the mouse is clicked, the point of the cursor's position doesn't intersect any of the position attributes (x, y, height, & width) I have for the object holding the actual image.
Does anybody know how to get the actual position of an image relative to the screen instead of the panel?
So far I haven't found any solutions to this online.
Thanks.
You can use the SwingUtilities.convertPoint(...) method.
I would guess you need to convert the mouse point the coordinates of the viewport of the scroll pane.
Maybe an easier approach is to use a JList with a custom renderer. You can set the JList to wrap components horizontally. Then in you custom renderer you just add a Border to the selected item. Read the section from the Swing tutorial on How to Use Lists for more information and examples.

android: make portion of an imageview selectable

How do I make part of an image view selectable with a resizable rectangle? (like the crop functionality) .
the image view:
EDIT:
I have successfully drawn a rectangle on the image view,by using canvas. how do I make the drawn rectangle dragable by the user?
I suggest you to extend the ImageView. You can have a Rect variable in your class which you can use to determine the cropping area. Than you can override onTouchmethod to handle touch events to manipulate the cropping rectangle and override the onDrawto draw the cropping area over bitmap.
There is also this cropper library you can use to avoid all the hard work by the way. https://github.com/edmodo/cropper

Crop an Image in irregular shape

I need to crop an Image in irregular shape my app, i.e., I select a portion by finger and if I click crop button, the selected portion alone in the Image will be saved in SDCard.
How to achieve it?
You have to define a Path for the Shape you want to be cut out.
Then create a rectangle around it an set everything between your shape an the rectangle to transparent (Alpha Channel)
This source here should give you some hints:
https://github.com/lvillani/android-cropimage/blob/develop/src/com/android/camera/CropImage.java
Take a look at the onSaveClicked method, line 209
Refer this project:
https://github.com/coderush/FlexiCrop
It has the code to crop irregular shapes.

Categories