Font width of non-English fonts - java

I am trying to draw characters from Hindi on a plain white background and store the resulting image as a jpeg. I need to size the image dynamically so that it fits the text. Currently, image height is fixed by assuming a size of 35 pixels (font size has been set to 22). How do I fix image width?
So far, I have tried to set the image width to 35 pixels times the maximum length of the different lines of text. That has not worked and the images saved are very wide. I am using drawString method of the graphics class in Java.
The function that creates images:
public static void printImages_temp(List<String> list) {
/* Function to print translations contained in list to images.
* Steps:
* 1. Take plain white image.
* 2. Write English word on top.
* 3. Take each translation and print one to each line.
*/
String dest = tgtDir + "\\" + list.get(0) + ".jpg"; //destination file image.
int imgWidth_max = 410;
int imgHeight_max = 230;
int fontSize = 22;
Font f = new Font("SERIF", Font.BOLD, fontSize);
//compute height and width of image.
int img_height = list.size() * 35 + 20;
int img_width = 0;
int max_length = 0;
for(int i = 0; i < list.size(); i++) {
if(list.get(i).length() > max_length) {
max_length = list.get(i).length();
}
}
img_width = max_length * 20;
System.out.println("New dimensions of image = " + img_width + " " + img_height);
BufferedImage img = new BufferedImage(img_width, img_height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, img_width, img_height);
//image has to be written to another file.
for(int i = 0; i < list.size(); i++) {
g.setColor(Color.BLACK);
g.setFont(f);
g.drawString(list.get(i), 10, (i + 1) * 35);
}
//g.drawString(translation, 10, fontWidth); //a 22pt font is approx. 35 pixels long.
g.dispose();
try {
ImageIO.write(img, "jpeg", new File(dest));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File written successfully to " + dest);
}
My questions:
How do I get width of a non-Latin type character, given that the fonts used to render the text are generic?
Is there a way to get the width that applies to all UTF-8 characters?

Related

getRGB(x,y) in Java awt returns for each pixel same value

I tried to walk through an image and take each RGB color value from all pixels and process them. But I get for all pixels the same RGB value. So obviously that is wrong.
I used the getRGB(x,y) method of an bufferedimage object in Java awt.
Know anyone what's the problem here?
Edit:
I got the problem, there were some mistakes by converting the image to an buffered image. I didn't draw the image in the in the buffered image.
The Following code working now as intended.
public void printImgDetails(Image img) {
// get the sizes of the image
long heigth = img.getHeight(null);
long width = img.getWidth(null);
// hashSet to hold all brightness values
HashSet<Float> set = new HashSet<Float>(0);
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
int rgb;
float[] hsv = new float[3];
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
for (int i = 0; i < width; i++) {
for (int j = 0; j < heigth; j++) {
Color c = new Color(bimage.getRGB(j, i));
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
Color.RGBtoHSB(r, g, b, hsv);
System.out.println("r: " + r + " g: " + g + " b: " + b);
set.add(hsv[2]);
}
}
// calculate the average brightness
double sum = 0;
for (float x : set) {
sum += x;
}
double avg = sum / set.size();
// print the results
System.out.println("avg --> " + avg);
}
Thanks in advance.
As I wirte in the edit, there was an problem by converting between image and bufferedimage. I forgot to draw image into the bufferediamge. Thats it.
If you get the same values for every pixel there are several possible reasons.
a) your image has the same values in every pixel
b) you do not change x and y between your calls to getRGB
c) you read something else but the return value of getRGB

How do I make my WritableRaster and pixelsize bigger in java?

I have a txt file with nrows, ncols etc and ex elevation values as numbers which we are supposed to make a map image of.
I have code (that works) but my problem is that the pixels are exactly one pixel big each, they actually have a cellsize also that already is defined(10m). When the code runs I get the smallest greyscale map ever, I want it to be atleast 10x10 cm big imageicon so I can se what's going on but I don't know where to set it please help. Already searched for ways to resize etc but noone really fit me, what about a setpixels solution?
public void mapColor()
{
int height = nRows; int width = nCols;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = image.getRaster();
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
double value = values[i][j];
int[] color = new int[3];
int colValue = getColorValue(value); //In this case Green to Red strength
int percentage = ((100*colValue)/255);
int R; int G; int B = 0;
//Formula: Red= 255*percentage /100 G= (255*(100-percentage))/100
R = (255*percentage)/100; G=(255*(100-percentage))/100;
R = Math.round(R); G= Math.round(G);
color[0]= R; color[1] = G; color[2] = B; //100% will be bright red and 50% yellow
raster.setPixel(j, i, color);
//System.out.println("Value " + value + " Red is " + R + " Green is " + G);
}
}
JFrame jf = new JFrame();
JLabel jl = new JLabel();
ImageIcon ii = new ImageIcon(image);
jl.setIcon(ii);
jf.add(jl);
jf.setSize(200, 200);
jf.setVisible(true);
}
This returns an image that is 2x2mm big, values is a double array with all elevation data. It's my first java course ever I want easy solutions.
The most simple solution would be to use the getScaledInstance method
and use the resulting image to create the ImageIcon:
Image scaledImage = image.getScaledInstance(200 * nRows, 200 * nCols, Image.SCALE_DEFAULT);
ImageIcon ii = new ImageIcon(scaledImage);

Java Grayscale Images

I've been working towards grayscaling images in java for some time. I was using a colorConvertOp, but it seems after a load of images were put through the process, eventually the JVM would hang in a locked state in the op.
Now I've started using:
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
However, I'm seeing a large spike in CPU, where it used to be under 20% and is now up to 120%. It also seems to be causing me memory leaks and eventually OOMs.
Is there an easier, quicker way to grayscale in java without using as much CPU/eliminates hanging from a JVM bug?
I wrote a java program to convert RGB image to GrayScaleImage. Hope this helps
public class GrayScale {
BufferedImage image;
int width;
int height;
public GrayScale() {
try {
File input = new File("input path of the image");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color c = new Color(image.getRGB(j, i));
int red = (int) (c.getRed() * 0.299);
int green = (int) (c.getGreen() * 0.587);
int blue = (int) (c.getBlue() * 0.114);
Color newColor = new Color(red + green + blue,
red + green + blue, red + green + blue);
image.setRGB(j, i, newColor.getRGB());
}
}
File ouptut = new File("output path of the image");
ImageIO.write(image, "jpg", ouptut);
} catch (Exception e) {
}
}
static public void main(String args[]) throws Exception {
GrayScale obj = new GrayScale();
}
}

Convert 2D array in Java to Image

I need to convert a 2D array of pixel intensity data of a grayscale image back to an image. I tried this:
BufferedImage img = new BufferedImage(
regen.length, regen[0].length, BufferedImage.TYPE_BYTE_GRAY);
for(int x = 0; x < regen.length; x++){
for(int y = 0; y<regen[x].length; y++){
img.setRGB(x, y, (int)Math.round(regen[x][y]));
}
}
File imageFile = new File("D:\\img\\conv.bmp");
ImageIO.write(img, "bmp", imageFile);
where "regen" is a 2D double array. I am getting an output which is similar but not exact. There are few pixels that are totally opposite to what it must be (I get black color for a pixel which has a value of 255). Few gray shades are also taken as white. Can you tell me what is the mistake that I am doing?
Try some code like this:
public void writeImage(int Name) {
String path = "res/world/PNGLevel_" + Name + ".png";
BufferedImage image = new BufferedImage(color.length, color[0].length, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 200; x++) {
for (int y = 0; y < 200; y++) {
image.setRGB(x, y, color[x][y]);
}
}
File ImageFile = new File(path);
try {
ImageIO.write(image, "png", ImageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedImage.TYPE_BYTE_GRAY is unsigned and non-indexed. Moreover,
When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded, as described in the AlphaComposite documentation.
At a minimum you need to preclude sign extension and mask off all but the lowest eight bits of the third parameter to setRGB(). Sample data that reproduces the problem would be dispositive.

Drawing on a transparent image using Java SWT

How do I create an in-memory fully transparent SWT image and draw a black line on it with antialias enabled?
I expect the result to include only black color and alpha values ranging from 0 to 255 due to antialias...
I googled and tried everything that I could... is this possible at all?
This is how I did and it works:
Image src = new Image(null, 16, 16);
ImageData imageData = src.getImageData();
imageData.transparentPixel = imageData.getPixel(0, 0);
src.dispose();
Image icon = new Image(null, imageData);
//draw on the icon with gc
I was able to make this work, although it feels a bit hacky:
Display display = Display.getDefault();
int width = 10;
int height = 10;
Image canvas = new Image(display, width, height);
GC gc = new GC(canvas);
gc.setAntialias(SWT.ON);
// This sets the alpha on the entire canvas to transparent
gc.setAlpha(0);
gc.fillRectangle(0, 0, width, height);
// Reset our alpha and draw a line
gc.setAlpha(255);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.drawLine(0, 0, width, height);
// We're done with the GC, so dispose of it
gc.dispose();
ImageData canvasData = canvas.getImageData();
canvasData.alphaData = new byte[width * height];
// This is the hacky bit that is making assumptions about
// the underlying ImageData. In my case it is 32 bit data
// so every 4th byte in the data array is the alpha for that
// pixel...
for (int idx = 0; idx < (width * height); idx++) {
int coord = (idx * 4) + 3;
canvasData.alphaData[idx] = canvasData.data[coord];
}
// Now that we've set the alphaData, we can create our
// final image
Image finalImage = new Image(canvasData);
// And get rid of the canvas
canvas.dispose();
After this, finalImage can be drawn into a GC with drawImage and the transparent parts will be respected.
I made it by allocating an ImageData, making it transparent then creating the Image from the data :
static Image createTransparentImage(Display display, int width, int height) {
// allocate an image data
ImageData imData = new ImageData(width, height, 24, new PaletteData(0xff0000,0x00ff00, 0x0000ff));
imData.setAlpha(0, 0, 0); // just to force alpha array allocation with the right size
Arrays.fill(imData.alphaData, (byte) 0); // set whole image as transparent
// Initialize image from transparent image data
return new Image(display, imData);
}
To scale with transparency, I've found that I have to manually set the alpha byte array as shown below. So the alpha ends up with nearest-neighbor anti aliasing.
public static Image scaleImage(Device device, Image orig, int scaledWidth, int scaledHeight) {
Rectangle origBounds = orig.getBounds();
if (origBounds.width == scaledWidth && origBounds.height == scaledHeight) {
return orig;
}
ImageData origData = orig.getImageData();
ImageData imData = new ImageData(scaledWidth, scaledHeight, origData.depth, origData.palette);
if (origData.alphaData != null) {
imData.alphaData = new byte[imData.width * imData.height];
for (int row = 0; row < imData.height; row++) {
for (int col = 0; col < imData.width; col++) {
int origRow = row * origData.height / imData.height;
int origCol = col * origData.width / imData.width;
byte origAlpha = origData.alphaData[origRow * origData.width + origCol];
imData.alphaData[row * imData.width + col] = origAlpha;
}
}
}
final Image scaled = new Image(device, imData);
GC gc = new GC(scaled);
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.setBackground(device.getSystemColor(SWT.COLOR_WHITE));
gc.fillRectangle(0, 0, scaledWidth, scaledHeight);
gc.drawImage(orig, 0, 0, origBounds.width, origBounds.height, 0, 0, scaledWidth, scaledHeight);
gc.dispose();
return scaled;
}

Categories