Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to crop image using mouse in JAVA swing

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a project in swing. In which i want to crop image using mouse onto a panel . My image size is very large so when i fit  image  onto a panel and cropping it then i am getting blur image.
And when i use resize image coordinate and cropping from original image then i am getting wrong cropping image . so i want to fixed image onto a panel and crop image should be perfect.
How can i do this? i am using this code :

           int x = Math.min(p1.x, p2.x);
           int y = Math.min(p1.y, p2.y);
           int w = Math.abs(p1.x - p2.x);
           int h = Math.abs(p1.y - p2.y);
           double xScale=image.getWidth()/mf.jPanel2.getWidth();
           double yScale=image.getHeight()/mf.jPanel2.getHeight();
           int x1 = (int) Math.round(x * xScale);
           int y1 = (int) Math.round (y * yScale);
           int w1=     (int) Math.round(w * xScale);
           int h1=     (int) Math.round(h * yScale);
           BufferedImage dest = image.getSubimage(x1,y1,w1,h1);
 
Saloon Keeper
Posts: 3406
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Priya,

welcome to the ranch and enjoy the stay!

I haven't ckecked your formulas, but here is what I have in my cropping program.

First of all, I assume that the displayed part of the image covers the panel fully, so that we do not face the risk of drawing  cropping rectangle with the mouse that lies partly outside the image (although it is possible to deal with that too, if so whished). That means that if the original image is smaller than the panel, it is enlarged enough, either with or without taking the aspect ratio into account.
In the rest I use the notation:
xL for the left most x coordinate, yT for the top y coordinate, and similar xR and yB for the right X and bottom Y coordinate. Note that in the standard coordinate system for a JPanel yB is usually higher than yTop, since the positive y axis goes from top to bottom.

Now, lets say that the image is displayed at these left and bottom right coordinates: (DxL, DyT) - (DxR, DyB).
The JPanel has coordinates (0, 0) - (PxR, PyT)  (these last ones are just the width and height of the panel, although I neglect the -1 here)
the Mouse cropping rectangle has coordinates (MxL, MyT) - (MxR, MyB)

If the original image has width W and height H, we get:
C(rop)xL = (MxL - DxL) / (DxR - DxL) * W
CyT = (MyT - DyT) / (DyB - DyT) * H
CxR = (MxR - DxL) / (DxR - DxL) * W
CyB = (MyB - DyB) / (DyB - DyT) * H

Beware of integer division, though!

Since in this way the displayed cropped image is smaller than the panel, you should enlarge it so that it fits the panel again.
It is your responsibility to make sure that you have correct values for the Dx's and the Dy's at all times. For instance, if your image is way larger than your panel, then you have the choice of squeezing your image so that it fits the panel, or showing only a part of the image. In both cases, make sure your Dx's and Dy's are matching.


 
priya shri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am getting a decimal value by which i am getting a wrong cropping. for example my image width is 10006 and  this image fit into a panel  that width is 1000 , MXL=10,MYT=10,MXR=30,MYB=30,
then cropx=(10-0/1000-0) * 10006 =100.06
 
Piet Souris
Saloon Keeper
Posts: 3406
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that is correct. Note that what you call 'cropx' is my C(rop)xL' variable. In likewise fashion we get that CxR = (30 - 0) / (1000 - 0) * 10006 = 300.18. Therefore the width of the cropped image = 300.18 - 100.06 = 200.12.

The cropped image will be the result of originalImage.getSubimage(100, CyT, 200, CyB - CyT) (the integer values are the result of adding 0.5 to the doubles, and casting to integer, or whatever rounding you want to apply).

Suppose that you display your image at the full size in your panel, with the left upper corner placed at the left upper corner of the panel. Then with the given mouse rectangle, the maths are:
CxL = (10 - 0) / (10006 - 0) * 10006 = 10, CxR = 30, et cetera.

In my program, it is possible that the displayed image has coordinates outside of the panel, for instance, if the image is way larger than the panel and is displayed at full size, the user can drag the image anywhere he/she wants, the panel showing thus different parts of the image, like using a ScrollPanel.

If I have msunderstood something, then please let me know.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!