I have an image that I want to show some 'spotlights' on, like they do on TV. The rest of the image should be darker than the original, and the person that I'm spotlighting should be normal. I have the x,y and radius of the spotlight, but I'm not sure how to change the brightness at that location.
Also, if I have two spotlights and they intersect, the intersection should be brighter than either of the spotlights.
Use RescaleOp on the original image and subimages. Given that you have a buffered image (called biDest) that contains the image, call RescaleOp(0.6, 0, null) on it to make it darker. Then, to add a (rectangular) spotlight, call the following:
public void spotLight(int x, int y, int w, int h)
{
BufferedImage i = biDest.getSubimage(x, y, w, h);
RescaleOp rescale = new RescaleOp(SPOTLIGHT_BRIGHTNESS, 0, null);
rescale.filter(i, i);
repaint();
}
A simple way is to convert the color to HSL, lower L to darken, increase to lighten, then convert back to RGB and set the pixel.
http://www.mpa-garching.mpg.de/MPA-GRAPHICS/hsl-rgb.html
Related
I want to get the color for specific coordinates inside a Canvas. I already tried getting a snapshot using this code:
WritableImage snap = gc.getCanvas().snapshot(null, null);
snap.getPixelReader().getArgb(x, y); //This just gets the color without assigning it.
But it just takes too much time for my application. I was wondering if there is any other way to access the color of a pixel for which I know the coordinates.
A Canvas buffers the drawing instructions prescribed by invoking the methods of a GraphicsContext. There are no pixels to read until the Canvas is rendered in a later pulse, and the internal format of the instruction buffer is not exposed in the API.
If a snapshot() of the Canvas is feasible, a rendered pixel may be examined using the resulting image's PixelReader.
int aRGB = image.getPixelReader().getArgb(x, y);
This example focuses on a single pixel. This example displays the ARGB BlendMode result in a TextField and a Tooltip as the mouse moves on the Canvas. More examples may be found here.
As an alternative, consider drawing into a BufferedImage, illustrated here, which allows access to the image's pixels directly and via its WritableRaster. Adding the following line to this complete example outputs the expected value for opaque red in ARGB order: ffff0000.
System.out.println(Integer.toHexString(bi.getRGB(50, 550)));
public class Pixel
{
private static final SnapshotParameters SP = new SnapshotParameters();
private static final WritableImage WI = new WritableImage(1, 1);
private static final PixelReader PR = WI.getPixelReader();
private Pixel()
{
}
public static int getArgb(Node n, double x, double y)
{
synchronized (WI)
{
Rectangle2D r = new Rectangle2D(x, y, 1, 1);
SP.setViewport(r);
n.snapshot(SP, WI);
return PR.getArgb(0, 0);
}
}
public static Color getColor(Node n, double x, double y)
{
synchronized (WI)
{
Rectangle2D r = new Rectangle2D(x, y, 1, 1);
SP.setViewport(r);
n.snapshot(SP, WI);
return PR.getColor(0, 0);
}
}
}
double degPi = degrees * Math.PI / 180;
double a = Math.cos(degPi)*tImgCover.getScaledHeight();
double b = Math.sin(degPi)*tImgCover.getScaledWidth();
double c = -Math.sin(degPi) * tImgCover.getScaledHeight();
double d = Math.cos(degPi)* tImgCover.getScaledWidth();
double e = absX;
double f = absY;
contentByte.addImage(imgae, a, b, c, d, e, f);/*add image*/
How to rotate around the image center by itext?
If we have an Image image and coordinates x, y, we can draw the image without rotation with its lower left corner at the given coordinates like this
contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);
A bitmap image from the resources has a size of 1x1 with the coordinate origin at its lower left. Thus, this operation stretches the image to its correct size and moves it so its lower left is at the given coordinates.
If we want to draw the same image as if the one drawn above was rotated around its center by an angle rotate, therefore, we can do this by moving the 1x1 image so that the origin is in its center, stretch it to its correct size, rotate it, and then move the origin (which still is at the center of the rotated image) to the center of the unrotated image. These operations are easier to express using AffineTransform instances (from package com.itextpdf.awt.geom) instead number tupels. Thus:
// Draw image as if the previous image was rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Stretch it to its dimensions
AffineTransform B = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform C = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform D = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
//Draw
contentByte.addImage(image, M);
(AddRotatedImage.java test method testAddRotatedImage)
For example drawing both images using
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;
results in something like this:
With a Flip
The OP asked in a comment
how to add rotate and flip image?
For this you simply insert a mirroring affine transformation into the sequence of transformations above.
Unfortunately the OP did not mention which he meant a horizontal or a vertical flip. But as changing the rotation angle accordingly transforms one in the other, that isn't really necessary, either.
// Draw image as if the previous image was flipped and rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Flip it horizontally
AffineTransform B = new AffineTransform(-1, 0, 0, 1, 0, 0);
// Stretch it to its dimensions
AffineTransform C = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform D = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform E = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
M.preConcatenate(E);
//Draw
contentByte.addImage(image, M);
(AddRotatedImage.java test method testAddRotatedFlippedImage)
The result with the same image as above:
With Interpolation
The OP asked in a yet another comment
How anti aliasing ?
The iText Image class knows an Interpolation property. By setting it to true (before adding the image to the document, obviously),
image.setInterpolation(true);
low resolution images are subject to interpolation when drawn.
E.g. using a 2x2 image with differently colored pixels instead of the image of Willi, you get the following results, first without interpolation, then with interpolation:
Confer the AddRotatedImage.java test testAddRotatedInterpolatedImage which adds this image:
Beware: iText Image property Interpolation effectively sets the Interpolate entry in the PDF image dictionary. The PDF specification notes in this context:
NOTE A conforming Reader may choose to not implement this feature of PDF, or may use any specific implementation of interpolation that it wishes.
Thus, on some viewers interpolation may occur differently than in your viewer, maybe even not at all. If you need a specific kind of interpolation on every viewer, upscale the image with the desired amount of interpolation / anti-aliasing before loading it into an iText Image.
public static BufferedImage rotateClockwise90( BufferedImage inputImage ){
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage returnImage = new BufferedImage( height, width , inputImage.getType() );
for( int x = 0; x < width; x++ ) {
for( int y = 0; y < height; y++ ) {
returnImage.setRGB( height-y-1, x, inputImage.getRGB( x, y ) );
}
}
return returnImage;
}
I have a BufferedImage and would like to set all pixels that are fully transparent to be fully-transparent white (rather than transparent blank, or whatever may be in the source file). I can of course loop through the entire image using getRGB and setRGB, but is there some other way that would be much faster?
You can set the pixels like this:
public void setRGB(int startX,
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)
This method sets an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image data. Color conversion takes place if the default model does not match the image ColorModel. There are only 8-bits of precision for each color component in the returned data when using this method. With a specified coordinate (x, y) in the this image, the ARGB pixel can be accessed in this way:
pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];
I can't say for sure if it is faster, but take a look at the ColorConvertOp class.
I haven't used it personally, but it might be what you are looking for.
I am trying to get a subImage of a BufferedImage and then use this subImage to replace the original BufferedImage, to implement a zooming feature based on a user specified selection. However, when I redraw the subImage onto the original image the bottom of the sub-image is not being displayed. I have no idea why. I was wondering if anyone could spot any obvious faults!?!
private void zoomImage(int x1, int x2, int y1, int y2){
BufferedImage subImage = originalImage.getSubimage(x1, y1, x2-x1, y2-y1);
Graphics2D graphics2D = (Graphics2D) originalImage.getGraphics();
graphics2D.drawImage(subImage, 0, 0, 600, 400, 0,0, x2-x1,y2-y1, null);
// clean up
graphics2D.dispose();
}
I was also wondering if there is a better way to implement zooming in on a particular rectangular selection and use the zoomed in section to replace the original image.
It should be noted that the size of sub image should not exceeds the original image width and height.
For example, if the originalimage.width=600 and orignalimage.height=800 then the sub image can be created whose size should be subimage.horizentalorigin+subimage.width<=600 and subimage.veticalorigin+subimage.height<=800.Other wise there will always be dark shadow found at the corners of the image.
subimage.horizentalorigin=x1,
subimage.verticalorigin=x2,
subimage.width=y1,
subimage.height=y2 in your case particularly.
The rest of calculation you do varies from algorithm to algorithm.....
I have an Image. On the bottom portion of the image, I would like to create a colored strip say, of height 100. I am already done with creating the strip and I can basically write strings in that portion (e.g. copyright of the image etc). The following is my method.
public static BufferedImage captionMyImage(BufferedImage sourceImage) {
int height=sourceImage.getHeight();
int width=sourceImage.getWidth();
System.out.println("Image Height: "+height);
System.out.println("Image Width: "+width);
int min=20; //fifty pixels
int newHeight=(int) (0.1*height>min ? 1.1*height : height+min);
int difference=newHeight-height;
System.out.println("new height:"+newHeight);
System.out.println("Difference:"+difference);
BufferedImage bgImage=new BufferedImage(width,newHeight,BufferedImage.TYPE_INT_RGB);
/**Create a Graphics from the background image**/
Graphics2D g = bgImage.createGraphics();
//g.drawRect(0, 0, width, ((int)0.1*height>min) ? (int)1.1*height : height+min);
g.setColor(Color.RED);
g.fillRect(0, 0, bgImage.getWidth(), bgImage.getHeight());
g.setColor(Color.YELLOW);
/**Set Antialias Rendering**/
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/**
* Draw background image at location (0,0)
* You can change the (x,y) value as required
*/
g.drawImage(sourceImage, 0, 0, null);
int strX=sourceImage.getWidth()/2;
int strY=sourceImage.getHeight()+difference/2;
System.out.println("X: "+strX);
System.out.println("Y: "+strY);
g.setFont(new Font(g.getFont().getName(),Font.PLAIN,20));
g.drawString("(c)www.sanjaal.com",strX ,strY);
g.dispose();
return bgImage;
}
I know the way I have calculated the values for x and y for drawString() method is just a simple one and has problem that the text goes outside the boundary sometimes (depending on image size and text length)
I would like to make sure that the text in the image on the bottom strip that I have defined always aligns to the right portion (boundary) of the image, but does not go out of boundary. How can I achieve that? Remember the text length can be dynamic.
Would the java Graphics experts out there share your ideas on how this can be achieved?
String msg = "(c)www.sanjaal.com";
int margin = 2;
g.drawString(msg, getWidth() - g.getFontMetrics().stringWidth(msg) - margin, strY);