Please suggest the way to take screenshot of URL/HTMLFile in java.
I am trying with LOBO Browser and able to Open URL in jframe but not able to take screenshot of content inside jframe.
Please check code sample
import org.lobobrowser.gui.FramePanel;
public LoboTestFrame() throws Exception {
FramePanel framePanel = new FramePanel();
this.getContentPane().add(framePanel);
framePanel.navigate("http://en.wikipedia.org/wiki/Main_Page");
}
Not able to capture loaded content as image
Yes. Combine Desktop#browse(), mentioned here, with Robot#createScreenCapture(), illustrated here and here.
Related
I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.
I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.
Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.
try this:
create a WebView and copy the text of your edittext in it:
webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");
and convert webview to pdf by below function:
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
if (printManager != null) {
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
after that user can select page size for example A4
There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.
The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.
- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).
check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2
and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591
- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.
They may be other solutions, but these are the popular ones.
Let Me know if it works for you and also if you're stuck. Thank you!
I came across functions in the OpenImaj library (LuoTangSubjectRegion and Achanta Saliency) that I would love to use them , however the problem is that Java is far from my first language. Therefore I wanted to ask if somebody could help me with trying to implement a simple piece of code that would read in an image, compute its saliency map and save that saliency map?
Cheers.
Here is sample. I am not sure it works, but i suspect it is closer than what you have.
I am not a maven expert. Apparently, you need maven to download the library. http://openimaj.org/UseLibrary.html. Sadly, this means I can't test this sample.
Good luck, for more code samples see http://openimaj.org/tutorial/processing-your-first-image.html.
import org.openimaj.image.MBFImage;
import org.openimaj.image.FImage;
//You will need several more imports. Your IDE can handle that.
public class SampleImage {
public static Main(String args[])
{
//Read image in
MBFImage image = ImageUtilities.readMBF(new File("c:\\file.jpg"));
//Print out random information
System.out.println(image.colourSpace);
//Create Object to preform work.
AchantaSaliency test = new AchantaSaliency();
//Get Saliency Map
test.analyseImage(image);
FImage newImage = test.getSaliencyMap();
//Display original image
DisplayUtilities.displayImage(image);
//Display new image
DisplayUtilities.displayImage(newImage);
//Save new image to file
ImageUtilities.write(newImage, new File("C:\\test_output.jpg"));
}
}
I have encountered a small problem that I need some help on. The issue is that I wish to call a browser window which calls a html page. The html file opens in 3 different browsers so the code for that should be correct. The actual problem is that it brings up a page can't be displayed error message
Here is the code that gets the location
package org.error;
public class BrowserLocation {
private String test1 = "org\\error\\PatientNumberError.html";
public BrowserLocation() {
}
public String patientNumberAddress() {
return test1;
}
}
and here is the code that creates the browser component and calls the location of the html file.
Browser browser = new Browser(container, SWT.NONE);
browser.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
browser.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
browser.setUrl(browserLocation.patientNumberAddress());
browser.setBounds(25, 25, 315, 180);
Would it be possible to find the error of my ways?
setUrl require a URL so you need something like:
browser.setUrl(new File(path).toURI().toURL().toString());
Sorry for not getting back to you earlier.
Someone that I know who is a senior Java programmer told me the problem that I was having was a case of absolute address versus relative address.
The reason for this is that if I was reading and writing to a file, then I would be able to use a relative address. However If I'm interacting with a server which is the case here as eventually It could go on-line (If I had the money) it would need to be an absolute address.
As I am still learning Java programming this was a very specific and important lesson to learn. I hope this would help anybody else who has had this issue.
I am trying to render and export FusionCharts completely on the server. I am aware of solutions such as FCimg and FusionCharts .NET Solution. I have also implemented a Java solution that uses the Process class to run wkhtmltoimage.
However, I am trying to find a pure Java solution of doing this. I have an html file that includes FusionCharts JS Libraries and code to generate the fusion chart. I found JxBrowser that properly renders the chart but it requires X-Server for it to work on Linux. I also have tried Cobra/Lobo Browser but it does not fully support JavaScript. Are there any other ways to render and export fusion charts on the server or atleast render an html file that includes JavaScript completely in Java (and that does not require xserver)?
Thanks in advance for all the help!
Update: Solution that does not require xserver: WebRenderer. The Swing Edition is the only edition that supports HTML5 as of July 9th, 2012. You can use the swing edition to capture the image without a GUI.
I found a way that uses Eclipse's SWT Browser. However this cannot be run in an headless mode. You will have to use xserver to implement this. See this question.
Since this requires xserver and cannot be run in an headless mode, I would suggest using JxBrowser. It is a lot simpler and all you need is to generate an html file with all the fusion charts scripts. See #1, #2, #3
You have to create a template.html file that contains the header
(<html><head>), jquery.min.js, FusionCharts.js,
FusionCharts.HC.js, FusionCharts.HC.Charts.js. Make sure each of
these scripts are in their own script tags (<script type="text/javascript"> [js code] </script>)
Now add another JavaScript function with its own script tags containing the steps to render the chart. For example:
function load() { FusionCharts.setCurrentRenderer('javascript'); var chart = new FusionCharts("swf", 'chart0', "width", "height", "0", "1"); chart.setXMLData("XML DATA HERE"); chart.render("divNAMEHere"); }
Now you need to call the load() function onload, create a div to render the chart in, and end the html file. For example:
`
test
`
Create a new class that imports the eclipse swt browser libraries. Instantiate Display, Shell, and Browser (use this as a guideline to help understand what is happening: http://www.roseindia.net/tutorials/swt/swt-browser.shtml).
Set the text of the browser (browser.setText("htmlcode")) to the html code from template.html. The best way to do this would be to read the file using BufferedReader.
Lastly, the image takes some time to render. Now there is probably a better way to do this but if you want to just get it working, I set up a count and it captures the image after a certain number. This is what you need to add to the end:
int i = 0;
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
i++;
// System.out.println(i);
if(i==100)
{
GC source = new GC (shell);
Image image = new Image(display, browser.getClientArea());
source.copyArea(image, 0, 0);
ImageLoader io = new ImageLoader ();
io.data = new ImageData[] { image.getImageData() };
File f = new File (currentDir+"/workpng.png");
io.save (f.getAbsolutePath(), SWT.IMAGE_PNG);
}
}
}
I have been trying to change the icon in the frame. I have virtually tried everything:
The icon is 16x16 which is the right size ....doesn't work
I've trying PNG,GIF and JPG formats none of them work.
Tried different way of setting the icon....doesn't work.
I've tried relative (local paths) e.g. "icon.gif" and absolute paths e.g. "c:\work\java\icon.gif" ...doesn't work
Here is my code and see if you can figure it out
Thanks
Oli
import javax.swing.*;
public class androidDriver
{
public static void main(String[] args) throws IOException
{
JFrame f = new JFrame("Android Data Viewer");
f.setResizable(false);
f.setSize(300,300);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setIconImage(new ImageIcon("androidIcon2.gif").getImage());
}
}
If you put the image in the same directory as the class file then the following should work for you:
f.setIconImage(new ImageIcon(androidDriver.class.getResource("androidIcon2.gif")).getImage());
Also would suggest setting the icon image before you make the frame visible
f.setIconImage(new ImageIcon(androidDriver.class.getResource("androidIcon2.gif")).getImage());
f.setVisible(true);
I suspect you may have to actually wait for the image to load using a MediaTracker. It's likely that the image is still loading at the point the frame setIconImage references it, so it does nothing.
Have you tried using Toolkit.getDefaultToolkit().getImage("androidIcon2.gif")
And two other things:
Does the image exist? The code you posted will fail silently.
Is it formatted properly? (though I assume Java could handle it if it wasn't)
Make a separate folder next to the source folder then put your image in there, and then use ImageIO to get the image like so:
f.setIconImage(ImageIO.read(new File("res/androidIcon2.gif")));
Also, if that doesn't work, try saving the image as a .png instead of a .gif.