Java BufferedImage padding - java

Is there any faster way to achieve padding of pixels to a BufferedImage than drawing it centered on larger BufferedImage?

BufferedImage has a constructor where you get to specify a WriteableRaster.
Picking at the a default buffered image, storing each pixel in an int, it uses an IntegerInterleavedRaster.
The ColorModel you can use ColorModel.getRGBDefault().
int imageWidth = 638, imageHeight = 480;
int dataImageWidth = 640;
SampleModel sm = new SinglePixelPackedSampleModel(TYPE_INT, imageWidth, imageHeight, dataImageWidth, new int[] { 0xff0000, 0xff00, 0xff });
DataBuffer db = new DataBufferInt(dataImageWidth * imageHeight);
WritableRaster r = Raster.createWritableRaster(sm, db, new Point());
BufferedImage image = new BufferedImage(ColorModel.getRGBDefault(), r, false, null);
Notice the scanlineStride in SinglePixelPackedSampleModel (second last parameter).
Another much simpler approach is to use BufferedImage's getSubimage method.
BufferedImage fullImage = new BufferedImage(dataImageWidth, imageHeight);
BufferedImage subImage = fullImage.getSubimage(0, 0, imageWidth, imageHeight);

Create an ImageIcon using the BufferedImage and add the Icon to a JLabel. Then you can just add a Border to the label to get your desired padding.

To defer centering until rendering, I like this approach due to finnw, where this is a suitable component:
private BufferedImage image;
....
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.translate(-image.getWidth() / 2, -image.getHeight() / 2);
g2d.drawImage(image, 0, 0, null);
}

Related

concat image file not able to create

I try to join two images. But not able to create it.
I am not able to understand what is the problem in the below code.
It is not able to create the concat.jpg file.
BufferedImage image = ImageIO.read(mainFile);
BufferedImage image1 = ImageIO.read(fileToMerge);
int width = Math.max(image.getWidth() , image1.getWidth());
int height = Math.max(image.getHeight() , image1.getHeight());
log.info("width {}", width);
log.info("height {}", height);
BufferedImage concatImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = concatImage.createGraphics();
graphics2D.drawImage(image, 0, image.getHeight(), null);
graphics2D.drawImage(image1, 0 , image1.getHeight(), null);
ImageIO.write(concatImage, "jpg", new File(Constants.LOCAL_FOLDER + "/concat.jpg"));
You want the height of concatImage to be the combined height of image and image1.
You want to draw image at coordinates (0,0) and you want to draw image1 at x = 0 and y = height of image
Try the following.
BufferedImage image = ImageIO.read(mainFile);
BufferedImage image1 = ImageIO.read(fileToMerge);
int width = Math.max(image.getWidth() , image1.getWidth());
int height = image.getHeight() + image1.getHeight();
log.info("width {}", width);
log.info("height {}", height);
BufferedImage concatImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = concatImage.createGraphics();
graphics2D.drawImage(image, 0, 0, null);
graphics2D.drawImage(image1, 0, image.getHeight(), null);
ImageIO.write(concatImage, "jpg", new File(Constants.LOCAL_FOLDER + "/concat.jpg"));
Note that you should call graphics2D.dispose() when you no longer need to use it.

Rotate BufferedImage with transparent background

I have an image with transparent background. I'd like to rotate this image to a specific angle and keep the transparent background for the resulting image. For this purpose I use the following method:
public static BufferedImage rotateImage(BufferedImage image, double angle, Color backgroundColor) {
System.out.println(image.getType());
double theta = Math.toRadians(angle);
double sin = Math.abs(Math.sin(theta));
double cos = Math.abs(Math.cos(theta));
int w = image.getWidth();
int h = image.getHeight();
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
Graphics2D g2d = tmp.createGraphics();
if (backgroundColor != null) {
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, newW, newH);
}
g2d.fillRect(0, 0, newW, newH);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.translate((newW - w) / 2, (newH - h) / 2);
g2d.rotate(theta, w / 2, h / 2);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return tmp;
}
I invoke it with background=null:
BufferedImage image = ImageIO.read(file);
rotateImage(image, 4, null);
ImageIO.write(bi, "PNG", new File("image.png"));
but the background of the resulting image.png is WHITE. What am I doing wrong and how to properly keep the transparent background for image.png?
I'm a bit puzzled about the behavior of Graphics.drawImage(). Maybe somebody else can comment about it.
However, Graphics2D.drawRenderedImage() works a treat. It takes an AffineTransform to control the rotation. The below example nicely works. You probably have additional requirement about the final image size and the location of the rotated image.
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageRotation {
public static void main(String[] args) {
ImageRotation rotation = new ImageRotation();
rotation.rotate("input.png", 45, "output.png");
}
public void rotate(String inputImageFilename, double angle, String outputImageFilename) {
try {
BufferedImage inputImage = ImageIO.read(new File(inputImageFilename));
BufferedImage outputImage = rotateImage(inputImage, angle);
ImageIO.write(outputImage, "PNG", new File(outputImageFilename));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private BufferedImage rotateImage(BufferedImage sourceImage, double angle) {
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = destImage.createGraphics();
AffineTransform transform = new AffineTransform();
transform.rotate(angle / 180 * Math.PI, width / 2 , height / 2);
g2d.drawRenderedImage(sourceImage, transform);
g2d.dispose();
return destImage;
}
}
Update
While the above code works for most PNGs, it does not work for the image that alexanoid is using. I've analyzed the image:
It's a grayscale image without a color palette (PNG color type 0) .
It uses simple transparency with a 2 byte long tRNS chunk.
As far as I can tell that's perfectly legal. However, ImageIO does not implement this combination. If the image has no palette, it simply ignores the tRNS chunk and therefore ignores the transparency information. That's most likely a bug.
You basically have two options now:
Look for an alternative library to read PNG files.
Fix the transparency after you have read the PNG file. This only works if know that the image used the particular problematic format.
Input and output for working PNG files
Input image:
Ouptput Image:

Buffered Image padding not working

I have a image of dimension 215*112. I want to make it 215*142.
Src Img:
I used the following code:
BufferedImage image = ImageIO.read(new File("src.png"));
int h = 15;
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight() + 2 * h, image.getType());
Graphics g = newImage.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, image.getWidth(), image.getHeight() + 2 * h);
g.drawImage(image, 0, h, null);
g.dispose();
ImageIO.write(newImage, "png", new File("dest.png"));
I am getting following result:
Why the padding is also getting added to x direction?
Because the source you are giving has two transparent vertical bars on the left and right of the image
Use this image

Black Padding with images in java?

I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation.
How could I remove it?
public static BufferedImage rotate(BufferedImage img, int angle) {
rotate_checked = false;
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg =new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
return dimg;
}
You need to create a transparent image:
BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);
where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.

How to change a bitmap's opacity?

I have a bitmap:
Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg");
But I'm not going to display the image to the user. I want the alpha to be 100 (out of 255). If this is not possible, can I set the opacity of the Bitmap?
As far as I know, opacity or other color filters can't be set on the Bitmap itself. You will need to set the alpha when you use the image:
If you're using ImageView, there is ImageView.setAlpha().
If you're using a Canvas, then you need to use Paint.setAlpha():
Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);
Also, incorporating WarrenFaith's answer, if you will use the Bitmap where a drawable is required, you can use BitmapDrawable.setAlpha().
You could also try BitmapDrawable instead of Bitmap. If this is useful for you depends on the way you use the bitmap...
Edit
As a commenter asked how he can store the bitmap with alpha, here is some code:
// lets create a new empty bitmap
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
// create a canvas where we can draw on
Canvas canvas = new Canvas(newBitmap);
// create a paint instance with alpha
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(42);
// now lets draw using alphaPaint instance
canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint);
// now lets store the bitmap to a file - the canvas has drawn on the newBitmap, so we can just store that one
// please add stream handling with try/catch blocks
FileOutputStream fos = new FileOutputStream(new File("/awesome/path/to/bitmap.png"));
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
public Bitmap makeTransparent(Bitmap src, int value) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap transBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(transBitmap);
canvas.drawARGB(0, 0, 0, 0);
// config paint
final Paint paint = new Paint();
paint.setAlpha(value);
canvas.drawBitmap(src, 0, 0, paint);
return transBitmap;
}
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
canvas.drawBitmap(bgr, 0, 0, transparentpainthack);
https://dzone.com/articles/adjusting-opacity-android proposes:
/**
* #param bitmap The source bitmap.
* #param opacity a value between 0 (completely transparent) and 255 (completely
* opaque).
* #return The opacity-adjusted bitmap. If the source bitmap is mutable it will be
* adjusted and returned, otherwise a new bitmap is created.
*/
private Bitmap adjustOpacity(Bitmap bitmap, int opacity)
{
Bitmap mutableBitmap = bitmap.isMutable()
? bitmap
: bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
int colour = (opacity & 0xFF) << 24;
canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
return mutableBitmap;
}
Note that with DST_IN you can modify (rather than reset) the transparency of already transparent image, that is, you can make the image more and more transparent.
If you are using a Drawable to display the image, you can change the alpha as follows:
private Drawable mTriangle;
mTriangle = context.getResources().getDrawable(R.drawable.triangle_arrow_for_radar);
...
protected void onDraw(Canvas canvas)
{
// Draw the triangle arrow
float imageTargetWidth = getWidth() / 15;
float scale = mTriangle.getIntrinsicWidth() / imageTargetWidth;
int imgWidth = (int)(imageTargetWidth);
int imgHeight = (int)(mTriangle.getIntrinsicHeight() / scale);
if (mTriangle != null)
{
mTriangle.setBounds(getWidth() / 2 - imgWidth / 2, getHeight() / 2 - imgHeight / 2, getWidth() / 2 + imgWidth / 2, getHeight() / 2 + imgHeight / 2);
mTriangle.setAlpha(150); // from (transparent) to 255 (opaque)
mTriangle.draw(canvas);
}
}

Categories