I'm trying to set the background color of my QR Code using iText into a transparent background, however it does not work. Shows only white bars and black background.
What i have done so far:
My Code Snippet:
PdfContentByte cb = writer.getDirectContent();
BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
java.awt.Image qrImage = qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
Image finalImage = Image.getInstance(writer, qrImage, 1);
finalImage.setAbsolutePosition(positionX, positionY);
cb.addImage(finalImage);
I have already generated my QR code and produced a PDF, however, when using
qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
It does not produce an alpha background, instead it only shows a black background color.
I have also tried:
java.awt.Image qrImage =
qrcode.createAwtImage(Color.WHITE,Color.OPAQUE);
But obviously, my arguments are incorrect.
Help will be most appreciated, i've been working on this for a day now.
I have also tried Graphics, Graphics2g, converting it into BufferedImage.
Changing the assignment of finalImage to the following works:
Image finalImage = Image.getInstance(qrImage, null)
I don't know why using the getInstance method that takes a PdfWriter as first argument ruins the transparency, though...
I would solve this problem like this:
BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
Image image = qrcode.getImage();
Image mask = qrcode.getImage();
mask.makeMask();
image.setImageMask(mask);
document.add(image);
There may be an AWT solution too, but I'm more familiar with native PDF solutions than with using an AWT workaround.
Related
I am trying to remove a particular color( Grey in this case ) from an image using OpenCV & Java.
I want all the other parts of the image should be present in the output image
Here is what I have tried.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat original = Imgcodecs.imread("C:\\ABC\\App2.jpg");
Mat dst = new Mat();
Core.inRange(original, new Scalar( 88, 88, 88), new Scalar( 88, 88, 88), dst);
Imgcodecs.imwrite("C:\\ABC\\mask4.jpg", dst);
Original Image: https://freeimage.host/i/dkdqXe
Original Output: https://freeimage.host/i/dkdukB
What it does is, the grey colored portion of the image is shown in white and the rest of the image is all black. But I just want to remove ( or make transparent ) the grey colored portion from the original image without affecting the other parts of the image. Any help pr input is much appreciated. Thanks in advance.
in Python you define the area by giving the range of the points:
import cv2
img=cv2.imread('test.jpg', -1)
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
Hope that can help you figuring in out in Java respectivly.
UPDATE #2
import cv2
img = cv2.imread('lena.jpg', -1)
roi = cv2.selectROI(img, False)
crop_img = img[roi[1]:(roi[1]+roi[3]), roi[0]:(roi[0]+roi[2])]
cv2.imwrite('cropped',crop_img)
I have a .png image, and I want to extract one part of that image using the PixelReader class, and rebuild it as an image :
Image image = new Image("file:ressources/spritesheets/Zelda_Overworld.png");
byte[] buffer = new byte[1024];
PixelReader pr = image.getPixelReader();
pr.getPixels(0, 0, 16, 16, PixelFormat.getByteBgraInstance(), buffer, 0, 64);
Image tile = new Image(new ByteArrayInputStream(buffer));
I can display image and buffer seems to contain values, but I can't display tile, tile.getPixelReader() returns null, tile.getWidth() and tile.getHeight() return 0.0.
Do you know what I am doing wrong?
Paul
Let WritableImage do this for you:
Image image = new Image("file:ressources/spritesheets/Zelda_Overworld.png");
Image tile = new WritableImage(image.getPixelReader(), x, y, width, height);
Depending on the use of tile doing this may not be necessary at all. ImageView has a viewport property that allows you to choose the part of the image to display and GraphicsContext provides an overloaded version of the drawImage method to draw a part of the image to the Canvas.
I've searched all around for this problem, but can't find a solution.
This is my render loop:
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
BitmapFont font = new BitmapFont(Gdx.files.internal("data/Media/font/myfont1.fnt"), false);
font.setColor(new Color(1, 1, 1, 1));
font.draw(batch, "Hello", 100, 100);
batch.end();
I've tried all possible colors, positions and different programs for generating fonts.
But the result is always the same: A black screen! (when glClearColor is (1, 1, 1, 1), a white screen...) Can anyone tell me what is wrong?
Thanks in advance!
EDIT:
I found the problem myself. It was a badly set up camera!
Seems that uhave not loaded the png file along with the fnt file
font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);
And please never try to load anything in render method.
Try to load the font in the constructor or else u will end up with a G.C call and f.p.s will eventually drop down
You can initallize a new BitmapFont without having .fnt in your project.
BitmapFont font = new BitmapFont();
then you render it:
batch.begin();
font.draw(batch, "Hello world", 200, 0);
batch.end();
don't forget that the Y axis starts from the bottom!
font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);
If you have few fonts images for example billy.fnt, billy_1.png, billy_2.png you can just use:
font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), false);
create a .fnt file using hiero which is provided by libgdx website
set the size of font 150 ,it will create a .fnt file and a png file
copy both of file in your assests folder
now declare the font
BitmapFont font;
nw in create method
font = new BitmapFont(Gdx.files.internal("data/rayanfont.fnt"), false);
//rayanfont is the font name you can give your font any name
in render
batch.begin();
font.setscale(.2f);
font.draw(batch, "hello", x,y);
batch.end();
this will work smoothly
I want to write text on the screen for my game, for things like fps, random text for items and stuff. How can I write that text?
Is it possible without the Basic Game class? Isn't there a command like this g.drawString("Hello World", 100, 100);?
Update: this answer is now outdated, and does not work at all with the latest versions of LWJGL. Until I update this answer fully, I recommend that you look here: https://jvm-gaming.org/t/lwjgl-stb-bindings/54537
You could use the TrueType fonts feature in the Slick-util library.
Using a common font is easy, just create the font like this:
TrueTypeFont font;
Font awtFont = new Font("Times New Roman", Font.BOLD, 24); //name, style (PLAIN, BOLD, or ITALIC), size
font = new TrueTypeFont(awtFont, false); //base Font, anti-aliasing true/false
If you want to load the font from a .ttf file, it's a little more tricky:
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont.deriveFont(24f); // set font size
font = new TrueTypeFont(awtFont, false);
} catch (Exception e) {
e.printStackTrace();
}
After you have successfully created a TrueTypeFont, you can draw it like this:
font.drawString(100, 50, "ABC123", Color.yellow); //x, y, string to draw, color
For more information, you can look at the documentation for TrueTypeFont and java.awt.Font, and the Slick-Util tutorial I got most of this code from.
Try Making a BufferedImage of required size. Then get its Graphics and draw a String. Then Convert it to a ByteBuffer and render it in OpenGL.
String text = "ABCD";
int s = 256; //Take whatever size suits you.
BufferedImage b = new BufferedImage(s, s, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = b.createGraphics();
g.drawString(text, 0, 0);
int co = b.getColorModel().getNumComponents();
byte[] data = new byte[co * s * s];
b.getRaster().getDataElements(0, 0, s, s, data);
ByteBuffer pixels = BufferUtils.createByteBuffer(data.length);
pixels.put(data);
pixels.rewind();
Now pixels contains the required Image data you need to draw.
Use GL11.glTexImage2D() function to draw the byte buffer.
I am re-sizing jpeg image from URL and store at some directory using JPEGImageEncoder in Java servlet.
Code is working fine in Our Development Solaris server. But it is storing image as black background color with square box.
Please help me for what can be the issue. Thanks in advance.
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
int quality = Integer.parseInt(nquality);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
-Manoj
I got the solution.
Actually source image url was not accessible from Java code. That was the reason I was getting black image. We change the url to accessible and now it works fine.
Thanks.