Rotate an Image in memory - java

I keep in memory a list of java.Awt Images and have the need to rotate them. I've read some solutions but they deal with changing the way the image is shown, not really rotating the image itself.
I need to rotate an image itself, not to draw in in a rotated way. How can this be attained?

The following code will rotate an image by an arbitrary angle in degrees.
Positive values for degrees will rotate the image clockwise, negative values counterclockwise.
The resulting image will be adjusted in size, so that the rotated image fits exactly into it.
I have tested it with jpg and png image files as input.
public static BufferedImage rotateImage(BufferedImage src, double degrees) {
double radians = Math.toRadians(degrees);
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
/*
* Calculate new image dimensions
*/
double sin = Math.abs(Math.sin(radians));
double cos = Math.abs(Math.cos(radians));
int newWidth = (int) Math.floor(srcWidth * cos + srcHeight * sin);
int newHeight = (int) Math.floor(srcHeight * cos + srcWidth * sin);
/*
* Create new image and rotate it
*/
BufferedImage result = new BufferedImage(newWidth, newHeight,
src.getType());
Graphics2D g = result.createGraphics();
g.translate((newWidth - srcWidth) / 2, (newHeight - srcHeight) / 2);
g.rotate(radians, srcWidth / 2, srcHeight / 2);
g.drawRenderedImage(src, null);
return result;
}

Related

PDFBox set Signature to Center of the Rectangle

Using PDFBox i am try to set signature to center on the box of rectacle, assume the rectacle size is 120x60 and can be customized and image signature can vary, examples 1000*1000 or 640x320 etc. I am try with resize the signature like the box :
// save and restore graphics if the image is too large and needs to be scaled
cs.saveGraphicsState();
cs.transform(Matrix.getScaleInstance(0.85f, 0.85f));
PDImageXObject img = PDImageXObject.createFromFileByExtension(imageFile, doc);
float wpercent = (rect.getWidth() / img.getWidth());
float newHeight = img.getHeight() * wpercent;
cs.drawImage(img, 0,0, rect.getWidth(), newHeight); //Resize image signature fit to rectangle and resize the height by percent to avoid stretch image
cs.restoreGraphicsState();
try to calculate coordinate inside the box
cs.drawImage(img, rect.getWidth()*0.10,rect.getHeight()*0.10, rect.getWidth(), newHeight);
example image got success set on center image size 1880x1000
if the signature just QR size 1000x1000
the QR got cut off
how to fix that ?
Solved refers to Auto resized image
by adding this function to scale image fit to rectangle
public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
int original_width = imgSize.width;
int original_height = imgSize.height;
int bound_width = boundary.width;
int bound_height = boundary.height;
int new_width = original_width;
int new_height = original_height;
// first check if we need to scale width
if (original_width > bound_width) {
//scale width to fit
new_width = bound_width;
//scale height to maintain aspect ratio
new_height = (new_width * original_height) / original_width;
}
// then check if we need to scale even with the new height
if (new_height > bound_height) {
//scale height to fit instead
new_height = bound_height;
//scale width to maintain aspect ratio
new_width = (new_height * original_width) / original_height;
}
return new Dimension(new_width, new_height);
}
then add to this function
Dimension scaledDim = getScaledDimension(new Dimension(img.getWidth(), img.getHeight()), new Dimension((int)rect.getWidth(), (int)rect.getHeight()));
int x = ((int) rect.getWidth() - scaledDim.width) / 2;
int y = ((int) rect.getHeight() - scaledDim.height) / 2;
cs.drawImage(img, x, y, scaledDim.width, scaledDim.height);

Rotating an image around a reference point [duplicate]

This question already has answers here:
Rotate a buffered image in Java
(4 answers)
Closed 1 year ago.
I'm trying to rotate the image around a reference point in Java using BufferedImage and AffineTransform, at first it seemed exactly what I needed, but it turns out it doesn't behave as expected. I need to do some rudimentary rotations, in multiples of 90, so I tried to do getQuadrantRotateInstance, but, if the reference point is at 0,0 then I get a RasterFormatException: Transformed height (0) is less than or equal to 0.
var rotation = switch (transform) {
case TRANS_NONE -> 0;
case TRANS_ROT90 -> 1;
case TRANS_ROT180 -> 2;
case TRANS_ROT270 -> 3;
default -> throw new NotImplementedException();
};
var transform = AffineTransform.getQuadrantRotateInstance(rotation, referenceX, referenceY);
var operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
var rotated = operation.filter(source, null);
By the looks of it the image gets rotated out of the canvas (into negative coordinates), resulting in exception above.
What would be the proper solution to create a rotated variant of the image without cropping or rotating around a center point like existing solutions do?
Rotating an image by an angle around the center point:
private BufferedImage rotateImage(BufferedImage buffImage, double angle) {
double radian = Math.toRadians(angle);
double sin = Math.abs(Math.sin(radian));
double cos = Math.abs(Math.cos(radian));
int width = buffImage.getWidth();
int height = buffImage.getHeight();
int nWidth = (int) Math.floor((double) width * cos + (double) height * sin);
int nHeight = (int) Math.floor((double) height * cos + (double) width * sin);
BufferedImage rotatedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = rotatedImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.translate((nWidth - width) / 2, (nHeight - height) / 2);
// This is the rotation around the center point - change this line
graphics.rotate(radian, (double) (width / 2), (double) (height / 2));
graphics.drawImage(buffImage, 0, 0, null);
graphics.dispose();
return rotatedImage;
}
To change the origin point of the rotation see javadoc of the method rotate.
Source: Creating simple captcha.

Move a matrix to the top right corner using pdfbox

Context :
I'm fairly new generating pdf and i'm scaling a matrix but my issue is that when the matrix gets scaled it then put my scaled matrix to the bottom left corner of the pdf.
My issue :
My problem is that actually when i cale my pdf it get stuck at the bottom left corner of the page and i can't find a way to put it on the top right corner whatever of the size of the scaled matrix.
My code :
float titleWidth = font.getStringWidth(title) / 1000 * fontSize;
float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
float width = page.getMediaBox().getWidth();
float height = page.getMediaBox().getHeight();
float ratio = Math.min(maxWidth/width, maxHeight /height);
float scaledWidth = width * ratio;
float scaledHeight = height * ratio;
page.setCropBox(PDRectangle.A4);
matrix.scale(sx = width / scaledWidth, height / scaledHeight);
contentStream.transform(matrix);
contentStream.beginText();
contentStream.newLineAtOffset(((page.getMediaBox().getWidth() - titleWidth) / 2), page.getMediaBox().getHeight() - marginTop - titleHeight);
contentStream.setFont(font, (float) (fontSize));
contentStream.showText(title);
contentStream.endText();
ByteArrayOutputStream os = new ByteArrayOutputStream();
contentStream.close();
document.save(os);
document.close();
return os.toByteArray();
This is a screenshoot of were the scaled matrix actually is (in case my explanation are not clean) :

How to create an image file with fixed size in Android?

I built a simple app that you can pick a photo of yourself, select a border for it and then save it as image. The way I did this is adding 2 ImageViews on top of each other in a parent view. Then converting this parent view to image and save. However with this way, the resulting image size depends on the device screen size. If the device screen is small, a small final image is generated.
What I want is creating an image file with a size of 500x800 pixels at all times regardless of device. What is the correct way to do this?
The preview on the screen could be small, but when I click save button I need it to be exactly 500x800.
First, convert your image to bitmap by :
Bitmap bitmapImage= BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
Or, if you have Uri of image then use this to convert it to bitmap:
Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath());
Both of this will give you a bitmap of your image.
Now to resize your image use this code:
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, true);
Then convert it into File.
To Add to Jason Answer, we found the best way to scale the image, is to use this to scale it up or down.
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
Please try to below code to save bitmap image as custom height & width, Below is a snippet of code that will allow you to resize a Bitmap.
public Bitmap getResizeBitmap(Bitmap bmp, int newHeight, int newWidth) {
int width = bmp.getWidth();
int height = bmp.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false);
return resizeBitmap;
}

java JLabel does not repaint resized label properly after 90 degree rotation

i have a use case like after adding JLabelComponent to pallet which i have to resize to custom level(re sizing bcz data is very large ) the added label component.Once i am done with re-sizing setting the component.setBounds all the coordinates. when i try to rotate the re sized label component to 90 degrees i am not getting proper shape. its head are cut off. please suggest
Here is my code:
if (selectedComponent instanceof LabelComponent) {
LabelComponent lbls = (LabelComponent) selectedComponent;
lbls.setAngle(Integer.parseInt(value));
lbls.repaint();
lbls.setSize(lbls.getPreferredSize());
and my paint method is
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
AffineTransform aT = g2.getTransform();
double sin = Math.abs(Math.sin(getAngle()));
double cos = Math.abs(Math.cos(getAngle()));
int originalWidth = getWidth();
int originalHeight = getHeight();
int newWidth = (int) Math.floor(originalWidth * cos + riginalHeight * sin);
int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin);
if(getAngle() == Integer.parseInt("90"))
{
g2.translate((newWidth-originalWidth)/2, (newHeight-orginalHeight)/2);
}
g2.rotate(Math.toRadians(getAngle()), originalWidth/2, originalHeight/2);
super.paint(g);
}
when i try to rotate the re sized label component to 90 degrees i am not getting proper shape.
One problem is that you need to reset the preferred size of the label. That is the width and height change because of the rotation.
Instead of doing custom painting you could try to use the Rotated Icon.

Categories

Resources