whats the difference of these two capture screen codes? - java

i have two codes of taking screenshots and i would like to know what is the difference between them?
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:\\failure.png"));
and
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
**BufferedImage fullImg = ImageIO.read(scrFile);**
FileUtils.copyFile(scrFile, new File("C:\\failure.png"));
so the row with ** ** is the difference but what she does? and why after her i can write:
Point point = element.getLocation();
int parkWidth = element.getSize().getWidth();
int parkHeight = element.getSize().getHeight();
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(),
point.getY(),parkWidth, parkHeight);
ImageIO.write(eleScreenshot, "png", scrFile);

i have two codes of taking screenshots and i would like to know what is the difference between them?
so the row with ** ** is the difference but what she does?
The difference is that in the 2nd code block you define a new variable fullImg. However, it is not used so those 2 code blocks are the same (for capturing the screenshot).
and why after her i can write:
This is because you have defined the variable fullImg and are then using it:
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),parkWidth, parkHeight);
Whereas in the first code block you don't have fullImg and thus this would error out.

Related

Capture specific content in a page as an image file [duplicate]

This question already has answers here:
How to capture screenshot of a WebElement within a webpage but not the entire screen or page through Selenium
(3 answers)
Closed 2 years ago.
My question is how to capture specific content in a web page to a screen shot but I couldn't. Lets consider any web page and if I'm trying to capture the content of any one class and wants to take a screenshot of just that class in Selenium, How can I do it! Do I need to consider dimensions and if so how do I do that. Please advice me how to do this.
I'm using the below functions in selenium:
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullimg = ImageIO.read(screenshot);
Point point = element.getLocation();
int elewidth = element.getSize().getWidth();
int eleheight = element.getSize().getHeight();
BufferedImage elementScreenshot = fullimg.getSubimage(point.getX(), point.getY(),elewidth,
eleheight);
ImageIO.write(elementScreenshot, "png", new File("Path"));
Second function:
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, "png", new File("Path"));
You need to get the WebElement you want to capture and with FileUtils from Apache you can save it as file.
Apache Common IO: https://mvnrepository.com/artifact/commons-io/commons-io/2.7
WebDriver driver = ...;
try{
driver.get("https://www.google.com/");
WebElement logo = new WebDriverWait(driver, 60).until(
ExpectedConditions.visibilityOfElementLocated(
By.xpath("//div[#id = 'main']")
)
);
File logoFile = logo.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(logoFile, new File("logo.jpg"));
}catch(Exception e){
e.printStackTrace();
}finally{
driver.quit();
}

javafx read javafx.scene.image.Image to ImageIO.write(), from CMYK to RGB

What I want to do is read an image from FileChooser and write it to file. I had to store the image in a javafx.scene.image.Image so that I can display it and clip it inside a circle. I have a little problem with trying to write the image that I got from javafx.scene.image.Image to file. The conversion process is not fluid, converts from CMYK to RGB (therefore turning my picture to some pink thing.
Please, I have checked a lot of other sources, and no one has been able to give me a notable solution
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(parent);
// get Image from selectedFile
Image userImg = = new Image( selectedFile.toURI().toURL().toString() );
if ( userImg != null ) {
String format = "jpg";
String filename = "d:\\pictureName."+ format;
File file = new File(filename);
// convert Image to BufferedImage
BufferedImage bufferedImage = SwingFXUtils.fromFXImage( userImg, null);
try {
// this is where i want to convert the color mode
// from cmyk to rgb, before i write it to file
ImageIO.write( bufferedImage, format, file );
} catch (IOException e) {
System.out.println("Exception :: "+ e.getMessage() );
}
}
Why do you think that there is some CMYK to RGB conversion happening? I suspect the reason for your "pink thing" is something different. The easiest way to find out is to change your output format from jpeg to png and see whether it makes a difference.
I think you are again one of the many people who are hit by this bug https://bugs.openjdk.java.net/browse/JDK-8119048 which is not considered important enough to be fixed. If you read the comments in there you will find a work-arround. Basically the idea is to copy the image after the conversion into a new image without alpha channel.
I'd really like to know how many more people have to waste their time until this bug finally gets enough attention to be fixed.

Take Screenshot of iframe Element using selenium

I am working on "selenium webdriver" with java. I can switch to iframe. I can read the Element which is inside iframe. But i want to take the screenshot of Element which is inside iframe. Am using the following code. Its not take screenshot inside iframe element. There is any solution for this.
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
Point point = ele.getLocation();
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,eleHeight);
ImageIO.write(eleScreenshot, imageType, screenshot);

How to get picture from *.docx with all changes using POI?

Let's review the relationship between "docx" and "pictures":
As I understand it, *.docx stores original pictures (pictures at the moment when you copy/paste them into Word). And every time when you use that picture, Word makes a "link" to original picture.
But if you make some changes to that picture (for example resize, crop or change color) Word remembers your changes, modifying the "link" (add some special tags). That's great, because you will never lose quality of your picture!
Let's get a picture from our *.docx file. To do that I use this code snippet:
XWPFDocument wordDoc = new XWPFDocument( pathToFile );
for (XWPFParagraph p : wordDoc.getParagraphs()) {
for (XWPFRun run : p.getRuns()) {
for (XWPFPicture pic : run.getEmbeddedPictures()) {
byte [] img = pic.getPictureData().getData()
File outputfile = new File ( pathToOutputFile );
BufferedImage image = ImageIO.read(new ByteArrayInputStream(img));
ImageIO.write(image , "png", outputfile);
}
}
}
But this way I get the original pictures from *.docx. If, for example, you cropped out a section from your picture and gave me the rest, then I always find the whole image in outputfile. That's not good.
Does anyone know how to get the picture with all changes that someone made to it in Word?

Convert PNG to bitonal TIFF

-Edit-
FYI.. I am converting b&w documents scanned in as greyscale or color.
1)The first solution worked, it just reversed black & white (black background, white text). It also took nearly 10 minutes.
2)The JAI solution in the 2nd answer didn't work for me. I tried it before posting here.
Has anyone worked with other libraries free or pay that handle image manipulation well?
-Original-
I am trying to convert an PNG to a bitonal TIFF using Java ImageIO. Has anyone had any luck doing this? I have got it to convert from PNG to TIFF. I am not sure if I need to convert the BufferedImage (PNG) that I read in or convert on the TIFF as I write it. I have searched and searched but nothing seems to work? Does anyone have an suggestions where to look?
Here is the code that converts...
public static void test() throws IOException {
String fileName = "4848970_1";
// String fileName = "color";
String inFileType = ".PNG";
String outFileType = ".TIFF";
File fInputFile = new File("I:/HPF/UU/" + fileName + inFileType);
InputStream fis = new BufferedInputStream(new FileInputStream(fInputFile));
ImageReaderSpi spi = new PNGImageReaderSpi();
ImageReader reader = spi.createReaderInstance();
ImageInputStream iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis, true);
BufferedImage bi = reader.read(0);
int[] xi = bi.getSampleModel().getSampleSize();
for (int i : xi) {
System.out.println("bitsize " + i);
}
ImageWriterSpi tiffspi = new TIFFImageWriterSpi();
TIFFImageWriter writer = (TIFFImageWriter) tiffspi.createWriterInstance();
// TIFFImageWriteParam param = (TIFFImageWriteParam) writer.getDefaultWriteParam();
TIFFImageWriteParam param = new TIFFImageWriteParam(Locale.US);
String[] strings = param.getCompressionTypes();
for (String string : strings) {
System.out.println(string);
}
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("LZW");
File fOutputFile = new File("I:\\HPF\\UU\\" + fileName + outFileType);
OutputStream fos = new BufferedOutputStream(new FileOutputStream(fOutputFile));
ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
writer.setOutput(ios);
writer.write(null, new IIOImage(bi, null, null), param);
ios.flush();
writer.dispose();
ios.close();
}
I have tried changing the compression to type "CCITT T.6" as that appears to be what I want, but I get an error " Bits per sample must be 1 for T6 compression! " Any suggestion would be appreciated.
Most likely, you want something like this to convert to 1 bit before you save to TIFF with CCITT compression.
To expound a little bit - be aware that converting from other bit depths to 1 bit is non-trivial. You are doing a data reduction operation and there are dozens of domain specific solutions which vary greatly in output quality (blind threshold, adaptive threshold, dithering, local threshold, global threshold and so on). None of them are particularly good at all image types (adaptive threshold is pretty good for documents, but lousy for photographs, for example).
As plinth said, you have to do the conversion, Java won't do it magically for you...
If the PNG image is already black & white (as it seems, looking at your comment), using a threshold is probably the best solution.
Somebody seems to have the same problem: HELP: how to compress the tiff. A solution is given on the thread (untested!).

Categories