JEditorPane and content width - java

I have some code where I take some text in html format, put it in a JEditorPane and create an image from it, as below:
String text = getMyText();
int height = getContentHeight(text);
int width = 850;
image = new BufferedImage(WIDTH, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
JEditorPane jep = new JEditorPane();
jep.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
jep.setContentType("text/html");
jep.setFont(new Font(myFont));
jep.setSize(width, height);
jep.setText(text);
jep.print(graphics);
graphics.dispose();
String fileName = "myFileName" + ".extension";
File outputfile = new File(myPath + "/" + fileName );
ImageIO.write(image, "png", outputfile);
This works wonderfully until the point I have some element in my html text with some fixed width (i.e. a div, or a table etc)
Since my original width is fixed at 850 pixels, if the width of that element is greater than that, what is happening is that the image looks like is being cut, meaning you cannot read certain text.
Is there a way so the text won't go over my fixed width? Or is it just a matter of making sure the width is within the limits beforehand?
Thanks for any help

Related

How can I set background fill of a run (a word in line or a paragraph) in a docx file by using Apache POI?

I know that it is possible to set the color of the run by: How can I set background colour of a run (a word in line or a paragraph) in a docx file by using Apache POI?
The relevant code is for example (see the answer in the referenced question):
XWPFRun run=paragraph.createRun();
run.setText("background color");
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setColor("auto");
cTShd.setFill("00FFFF");
I would like to use an image instead as a pattern, is it possible? I know that the argument used for the fill argument is an object, so is it possible to use a picture reference or something like that?
I also know how to add directly a picture in a docx document, for example:
Image image = // my image to use
float widthEMU = // the width of the image in EMU
float heightEMU = // the height of the image in EMU
BufferedImage bimage = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(_image, 0, 0, null);
bGr.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bimage, "png", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
XWPFPicture pic = run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, "Picture", widthEMU, heightEMU);

Image quality reduces (turns reddish) as JPEG with BufferedImage

I am making a program where I extract out pixel array from an image, Take out ARGB values. And write them back again to make another image.
BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();
pixels = new int[iw * ih];
PixelGrabber pg = new PixelGrabber(imagebuffer, 0, 0, iw, ih, pixels, 0, iw);
pg.grabPixels();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, pixels, 0, width);
ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));
Now output image for png and gif look fine but the output jpg image turns out quite reddish.
This is the original image
And this is the output jpg image
Any idea what might be causing this? Any push towards the right direction will be appreciated.
I dont know if this will work for you, but I always did it pixel-by-pixel.
So :
BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();
BufferedImage image = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_ARGB);
for (int x=0; x < iw; x++) {
for (int y=0; y < ih; y++) {
image.setRGB(x,y,imagebuffer.getRGB(x,y));
}
}
ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));
And it this way has a similar count of lines, so I think you could give it a try.
If you'd like to insert text, id ìmport java.awt.*;, what includes Graphics and Graphics2D and Font, and then :
Font font=new Font("Sans,0,20); //Name, type(none, bold, italic), size
Graphics2D imagegraphics=imagebuffer.createGraphics();
imagegraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //enable antialiasing
imagegraphics.setFont(font);
imagegraphics.setColor(Color.BLACK);
String yourtext="Fighter";
int h=imagegraphics.getFontMetrics().getHeight();
int w=imagegraphics.getFontMetrics().stringWidth(yourtext);
imagegraphics.drawString(yourtext,5,h); //Draw text upper left corner, note that y-value is the bottom line of the string
EDIT :
It was the Alpha value. Fixed by :
BufferedImage image = new
BufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB); //RGB, jpeg hasnt got alpha, ints have been converted as if they contain red first, but its alpha(the first bytes, these ints are interpreted bitwise i think) (argb), so it became more red.

Convert color image into black and white using java created from scratch

I want to convert my picture from colored to Black and white which seems to be created from scratch.
Here is the code which i tried as described on the different post:
BufferedImage bi = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
ImageIO.write(bi, "PNG", new File("/Users/bng/Documents/rendered2.png"));
op.filter(bi, bi);
But still my image is not converted to the Black and white. Additionally, this code is increasing the rendered2.png image size to 10 folds.
Also, it would be great if i could find some Java 8 way of doing this.
Any suggestions?
Here is the code which worked for me:
BufferedImage input = ImageIO.read(new File("/Users/bng/Documents/Photograph.jpg"));
// Create a black-and-white image of the same size.
BufferedImage im = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
// Get the graphics context for the black-and-white image.
Graphics2D g2d = im.createGraphics();
// Render the input image on it.
g2d.drawImage(input, 0, 0, null);
// Store the resulting image using the PNG format.
ImageIO.write(im, "PNG", new File("/Users/bng/Documents/rendered.png"));
It was BufferedImage.TYPE_BYTE_BINARY which provided me the exact solution.
Lokking for the Java 8 Version for above code.
You have to find RGB of the existing colors of the image you want to change it.
Fyi, you want to change it as white RGB value is (255,255,255) and for black RGB value is (0,0,0)
Following method easily do the color change if you apply correct way of your requirement
private BufferedImage changeColor(BufferedImage image, int srcColor, int replaceColor)
{
BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = destImage.createGraphics();
g.drawImage(image, null, 0, 0);
g.dispose();
for (int width = 0; width < image.getWidth(); width++)
{
for (int height = 0; height < image.getHeight(); height++)
{
if (destImage.getRGB(width, height) == srcColor)
{
destImage.setRGB(width, height, replaceColor);
}
}
}
return destImage;
}
you have to use the ColorConvertOp in a proper way:
create Source image
apply filter
save dest
example:
BufferedImage src = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage dest = op.filter(src, null);
ImageIO.write(dest, "PNG", new File("/Users/bng/Documents/rendered2.png"));
src:
dest:

Converting a color image to grayscale is showing black image in java

I am trying to convert an image to gray scale image using Java. The problem here is, when I am trying to save that image after converting into gray scale it is showing a black image. I am little unaware of this image conversions. I would like to know why it is not working, and your answers will help me in my project. Thank you.
BufferedImage image = ImageIO.read(fileContent);
File outputFile = new File(date1);
ImageIO.write(image1, "png", outputFile);
(This is the color image obtained from html page, and it is working properly. And when I am trying to display/save this image, color image is visible. But the problem arises when the below code is used to convert color image to gray scale image.)
BufferedImage image1 = new BufferedImage(width, height,BufferedImage.TYPE_BYTE_GRAY);
Graphics graphics = image1.getGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
File outputFile = new File(date1);
ImageIO.write(image1, "png", outputFile);
There are color space conversion issues. You can manually convert each pixel of the output image to grayscale and set the pixels of the output image this way:
int gray = red * 0.299 + green * 0.587 + blue * 0.114
or use the ColorConvertOp class to do the conversion for you:
BufferedImage in = ImageIO.read(inputFile);
BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
ColorConvertOp op = new ColorConvertOp(in.getColorModel().getColorSpace(), ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(in, out);
ImageIO.write(out, "png", outputFile);

Printing the line separator character in an image [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Problems with newline in Graphics2D.drawString
String eol =System.lineSeparator();
String sampleText =epcURNText +eol+studentName+eol+DelayComments+eol+ArrivalMethodComments+eol;
System.out.println(sampleText);
Font font = new Font("Tahoma", Font.PLAIN, 11);
FontRenderContext frc = new FontRenderContext(null, true, true);
//get the height and width of the text
Rectangle2D bounds = font.getStringBounds(sampleText, frc);
int w = (int) bounds.getWidth();
int h = (int) bounds.getHeight();
//create a BufferedImage object
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
//calling createGraphics() to get the Graphics2D
Graphics2D g = image.createGraphics();
//set color and other parameters
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(sampleText, (float) bounds.getX(),
(float) -bounds.getY());
//releasing resources
g.dispose();
// define the format of print document
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "gif", os);
File f = new File("MyFile.jpg");
ImageIO.write(image, "JPEG", f);
Within the aforementioned code I am trying to print a string with newline characters within an image. However the newline character is omitted thus my text is presented within a single line? Does anybody have any idea on how to fix this?
Render the text in a JLabel with HTML formatting as shown the LabelRenderTest source in this answer.
It provides something better than line-breaks - the style for body width. Better in the sense that we do not need to manually calculate where to put line breaks for text (which also might be rendered in fonts that are not fixed width).

Categories