I've wrote a bean that takes photos from a webcam. I'd like to display these image in a JSF 2.0 page and update them every n second.
If I give the path name the file in eclipse like this it work:
public String getNewPhoto() {
try {
File dir = new File("C:/Users/User/MyApp/images/");
FileUtils.cleanDirectory(dir);
}catch(Exception ex) {
ex.printStackTrace();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
try {
webcam = Webcam.getDefault();
webcam.open();
ImageIO.write(webcam.getImage(), "PNG", new File("C:/Users/User/MyApp/images/"+ timeStamp + ".png"));
webcam.close();
}catch(Exception ex) {
ex.printStackTrace();
}
return "C:/Users/User/MyApp/images/" + timeStamp + ".png";
}
With the following XHTML:
<p:graphicImage value="#{myBean.newPhoto}" id="photo" cache="false" />
<p:poll interval="1" listener="#{myBean.increment}" update="photo" />
As expect all of the above works fine from my dev environment on eclipse. I'd like to deploy this to my server (Linux). When I change the paths from what you see above to
/var/lib/tomcat7/webapps/MyApp/images
Then the images get saved, but I can't display them in h:graphicImage. I also tried passing:
http://hostname:8080/MyApp/images/....
to h:graphicImage and still no dice, I'm sure I'm missing something real simple. Any help is appreciated!
You need to change it in the image saving line as well . . .
ImageIO.write(webcam.getImage(), "PNG", new File("C:/Users/User/MyApp/images/"+ timeStamp + ".png"));
How about the following:
View
<p:graphicImage value="#{photoBean.newPhoto}" id="photo" cache="false" />
<p:poll listener="#{photoBean.updatePhoto}" interval="1"
update="photo" />
Managed Bean
#ManagedBean
#RequestScoped
public class PhotoBean {
private String realPath;
private String realtivePath;
#PostConstruct
public void init() {
realtivePath = "/images/webcam.png";
ExternalContext externalContext = FacesContext.getCurrentInstance()
.getExternalContext();
realPath = externalContext.getRealPath(realtivePath);
}
public void updatePhoto() {
try {
File file = new File(realPath);
file.delete(); // cleanup
// Use file object to write image...
} catch (IOException e) {
// Implement some exception handling here
e.printStackTrace();
}
}
public String getNewPhoto() {
return realtivePath;
}
}
Some more notes (getters)
It's not a good idea to process the webcam image in the getter (getNewPhoto) as getters may be called multiple times by JSF.
See: Why JSF calls getters multiple times
Timestamp
I've removed the timestamp from the filename. I think you've added it to prevent browser caching. This is not required as cache=false already creates unique image URIs.
Related
I am using external images in my webaplication, everything wass fine until I wanted to add animated gif there, the gif loads, but it doesn't animate.
Java code:
File sourceimage = new File("loading_img.gif");
try {
final BufferedDynamicImageResource r = new BufferedDynamicImageResource("GIF");
r.setImage(ImageIO.read(sourceimage));
add(new Image("gif", r));
} catch (Exception e) {
e.printStackTrace();
}
HTML:
<img wicket:id="gif"/>
EDIT:
Tried martin-g suggestion, gif still doesn't animate
try {
final BufferedDynamicImageResource r = new BufferedDynamicImageResource("GIF"){
#Override
protected void setResponseHeaders(AbstractResource.ResourceResponse data,
IResource.Attributes attributes){
super.setResponseHeaders(data, attributes);
data.setContentType("image/gif");
}
};
r.setImage(ImageIO.read(sourceimage));
add(new Image("gif", r));
} catch (Exception e) {
e.printStackTrace();
}
The problem is that the content type is not automatically set.
You will need to override org.apache.wicket.request.resource.AbstractResource#setResponseHeaders() and set with via resourceResponse.setContentType(String).
Maybe this should be done automatically by Wicket in org.apache.wicket.request.resource.DynamicImageResource, since it knows the format ("png", or "gif" as in your case). Please file a ticket in Wicket's JIRA for this improvement! Thanks!
I'm uploading an Excel Workbook to a directory which is outside the application context (for backup purposes) and saving its path into the database.
Now I need to download that file and I'm trying to use Primefaces Download, but I'm getting a null file.
Here is my code (much like Primefaces Showcase Download section):
Bean:
private StreamedContent file;
public void download(Arquivo arquivo) throws IOException {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance()
.getExternalContext().getContext()).getResourceAsStream(arquivo.getNomeArquivo());
file = new DefaultStreamedContent(stream);
}
View:
<h:commandLink id="btnDownload" title="Download Arquivo"
actionListener="#{arquivoBean.download(obj)}">
<p:fileDownload value="#{arquivoBean.file}" />
</h:commandLink>
Basically I need to pass an external path to InputStream instead of FacesContext.
From what I see my problem is that I'm passing my application context to the InputStream, appending the path in the argument of getResourceAsStream which, of course, is not found.
I'm new to this FileDownload thing. Thanks in advance!
If it can still be useful :
StreamedContent streamToReturn = DefaultStreamedContent.builder().name(FILE_NAME).contentType( FacesContext.getCurrentInstance().getExternalContext()
.getMimeType(FILE_PATH))
.stream(() -> {
try {
return new FileInputStream(FILE_PATH);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}).build();
I am using Primefaces p:graphicImage to display the image of every logged in users. Everything is working near fine except that my photos appear like negatives. What could I be doing wrong. Here is my code :
<sec:authorize access="isAuthenticated()">
<span>
<p:graphicImage value="#{currentUser.image}" class="img-thumbnail pull-right"
height="80px;" width="80px;" >
<f:param name="id" value="#{request.remoteUser}" />
</p:graphicImage>
</span>
.....
And here is the JSF Managed Bean
#Named(value = "currentUser")
#ApplicationScoped
public class CurrentUser implements Serializable {
#EJB
private VempDetailsFacade vempDetailsFacade;
private VempDetails details;
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
} else {
// So, browser is requesting the image. Return a real StreamedContent with the image bytes.
String imageId = context.getExternalContext().getRequestParameterMap().get("id");
details = vempDetailsFacade.find(imageId);
if (details != null) {
try {
return new DefaultStreamedContent(new ByteArrayInputStream(details.getEmpImage()));
} catch (Exception e) {
System.err.println("No Image Retrieved : "+e.getMessage());
}
}
return null;
}
}
public VempDetails getDetails() {
return details;
}
public void setDetails(VempDetails details) {
this.details = details;
}
#PostConstruct
public void init() {
details = vempDetailsFacade.
find(FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
}
}
What is it I could be doing wrong. The same pictures displayed in a normal swing application display without any problem
Update On Code
#BalusC
Here is the quick test code that displays the image on swing.
public DisplayImage() {
super("Image Display");
setSize(600, 600);
connection = getConnection();
try {
statement = connection
.prepareStatement("SELECT empImage FROM v_empDetails WHERE empCode=?");
statement.setString(1, "009");
result = statement.executeQuery();
byte[] image = null;
//just a result anyway
while (result.next()) {
image = result.getBytes(1);
}
Image img = Toolkit.getDefaultToolkit().createImage(image);
ImageIcon icon = new ImageIcon(img);
JLabel lPhoto = new JLabel();
lPhoto.setIcon(icon);
add(lPhoto);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setVisible(true);
}
public Connection getConnection() {
Connection connection = null;
........
Could it be that the Toolkit.getDefaultToolkit().createImage(image); line is doing the re-invert. The images were populated using a different application and this is more of an update to have it as a web-based application.
Thanks for your advice in advance.
JSF/PrimeFaces is just the presenter and doesn't invert the images at all. Otherwise anyone else in the world using JSF/PrimeFaces would have faced the same problem. Your problem lies deeper. It's more likely that the images are already stored as negatives in DB and that your existing Swing/Java2D code is written in such way that it re-inverts the images before displaying, perhaps during a badly written resizing/cropping process.
So, to fix the problem, refocus on the code responsible for storing those images in the DB (and don't forget to fix the Swing/Java2D code to not re-invert those anymore).
I am looking for a way to attach a screenshot to Results section of TestNG Report for the failed methods.
So far I was able to attache my screenshots to Reporter Output by implementing this:
Reporter.log("<br> <img src=.\\screenshots\\" + fileName + " /> <br>");
but still struggling with adding them to Test Results section of failed methods.
I was able to implement Listener and intercept onTestFailure actions which was originally suggested here:
How can I include a failure screenshot to the testNG report
Here is an example of that:
#Override
public void onTestFailure(ITestResult result) {
Reporter.setCurrentTestResult(result);
Reporter.log("<br> <img src=.\\screenshots\\Untitled.png /> <br>");
Reporter.setCurrentTestResult(null);
}
But Reporter.log function still pushes my information in the Reporter output log but not in the Results->Failed methods->Failed method log.
Update (03/14/14): I've attached screenshot to clarify my question. The problem is not in capturing screenshot and attaching it to Report. That part works fine. The problem is that screenshot is attached to Test Output part of the report but I want to see it in Results -> Failed Methods.
I have also implemented the same extending Testng TestListenerAdapter. By capturing the screenshot then attach it to the Testng Report with the image of size height=100 and width=100 with onTestFailure. Please see below if this helps solve your problem
File scrFile = ((TakesScreenshot) WebdriverManager.globalDriverInstance).getScreenshotAs(OutputType.FILE);
//Needs Commons IO library
try {
FileUtils.copyFile(scrFile, new File(file.getAbsolutePath()+ "/selenium-reports/html/" + result.getName() + ".jpg"));
Reporter.log("<a href='"+ file.getAbsolutePath()+"/selenium-reports/html/" + result.getName() + ".jpg'> <img src='"+ file.getAbsolutePath()+"/selenium-reports/html/"+ result.getName() + ".jpg' height='100' width='100'/> </a>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Reporter.setCurrentTestResult(null);
In addition to above don't forget to add following lines to your testng suite xml
<listeners>
<listener class-name="com.tests.DotTestListener" />
</listeners>
OR
passing as listner parameter if you are executing it from command line
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -listener com.tests.DotTestListener test\testng.xml
Reference : http://testng.org/doc/documentation-main.html#logging-listeners
I've had same problem but it solved. By implementing SuitePanel you will be able to add screenshot as you want
https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/reporters/jq/SuitePanel.java
I almost don't change the original code except
...
// Description?
String description = tr.getMethod().getDescription();
if (! Strings.isNullOrEmpty(description)) {
xsb.push("em");
xsb.addString("(" + description + ")");
xsb.pop("em");
}
// Add screen shot here
xsb.push(“img”,”src”,imagePath);
xsb.pop(“img”);
xsb.pop(D);
xsb.pop(D);
If you want to show the screen shot on the failed method then you will have to capture the exception and modify its message content and add img html to that and then it will appear. Let me know if you need example
I had same issue but its fixed now. I made a method to catcher screenshot in base class. this method return full path of screenshot.
public String getScreenshot (String screenshotName, WebDriver driver) throws IOException{
DateFormat dateformate = new SimpleDateFormat("dd-mm-yy-hh-mm-ss");
Date date = new Date();
String currentdate = dateformate.format(date);
String imageName =screenshotName+currentdate;
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
String location =System.getProperty("user.dir")+"\\testOutput\\screenshot\\"+imageName+".png";
File screenshotLocation =new File (location);
FileUtils.copyFile(source, screenshotLocation);
return location;
}
Add use this path to add screenshot in testng report as well as report log by updating testng TestNgListener-
public void onTestFailure(ITestResult arg0) {
Object currentClass = arg0.getInstance();
WebDriver driver = ((BrowserSetup) currentClass).getDriver();
String name = arg0.getName();
System.out.println(name);
try {
String screenshotPath =getScreenshot(name, driver);
System.out.println("Screenshot taken");
String path = "<img src=\"file://" + screenshotPath + "\" alt=\"\"/>";
System.out.println(screenshotPath+" and path - "+path);
Reporter.log("Capcher screenshot path is "+path);
} catch (Exception e) {
System.out.println("Exception while takescreenshot "+e.getMessage());
}
printTestResults(arg0);
}
I suggest you to use ReportNG instead of the primitive TestNG reports.
If the problem only with getting screenshots, you can try to get it like this:
private static byte[] GetCropImg(IWebDriver targetChrome, IWebElement targetElement)
{
var screenshot = ((ITakesScreenshot)targetChrome).GetScreenshot();
var location = targetElement.Location;
using (MemoryStream stream = new MemoryStream(screenshot.AsByteArray))
{
var rect = new Rectangle(location.X, location.Y, targetElement.Size.Width, targetElement.Size.Height);
using (Bitmap bmpImage = new Bitmap(stream))
{
using (Bitmap cropedImag = bmpImage.Clone(rect, bmpImage.PixelFormat))
{
using (MemoryStream ms = new MemoryStream())
{
cropedImag.Save(ms, ImageFormat.Jpeg);
byte[] byteImage = ms.ToArray();
return byteImage;
}
}
}
}
}
and then you can save file or save sting64
var imgString64 = Convert.ToBase64String(byteImage); //Get Base64
PS: on JAVA it should be almost the same)
you can get failed test cases screen shots with name of failed test class by using #After method.
Use below code segment
I'm using Flying Saucer to create a pdf from xhtml, hosted on a tomcat server. Most of the images included in the pdf are publicly available (logos and so on), but some of them are protected behind a login (that is, they are streamed through a servlet if the user is logged in).
When I paste the url in the browser, the image is of course displayed fine, because the browser sends the session with the request. But when Flying Saucer renders the pdf, it doesn't include the protected image because it doesn't know anything about the session.
So, my question is; is there any way to include the byte streams for Flying Saucer to resolve, just as it is possible to add resolvable fonts? I have tried something like this, but there is no easy way to set the UAC on the ITextRenderer, and it complained every time i tried.
You can set the UserAgentCallback this way, and Flying Saucer will use it to resolve the urls (tested, works with Release 8):
ITextRenderer renderer = new ITextRenderer();
renderer.getSharedContext().setUserAgentCallback(new MyUAC());
MyUAC should extend the NaiveUserAgent, and override the resolveAndOpenStream method as the other page suggests.
I overrode ITextUserAgent as well - from the source, looks like that's what ITextRenderer uses. You have to provide the output device in the constructor, which you can get from the renderer object. One other gotcha was you have to set the "shared context" explicitly using the setter method - otherwise you will get an NPE during rendering. Here is the code to set up the object:
ITextRenderer renderer = new ITextRenderer();
MyUserAgentCallback uac = new MyUserAgentCallback(renderer.getOutputDevice());
uac.setSharedContext(renderer.getSharedContext());
renderer.getSharedContext().setUserAgentCallback(uac);
Also, here is the basic idea of MyUserAgentCallback, using basic authentication:
private static class MyUserAgentCallback extends ITextUserAgent
{
public MyUserAgentCallback(ITextOutputDevice outputDevice)
{
super(outputDevice);
}
#Override
protected InputStream resolveAndOpenStream(String uri)
{
if (_isProtectedResource(uri))
{
java.io.InputStream is = null;
uri = resolveURI(uri);
try {
URL url = new URL(uri);
String encoding = new BASE64Encoder().encode ("username:password".getBytes());
URLConnection uc = url.openConnection();
uc.setRequestProperty ("Authorization", "Basic " + encoding);
is = uc.getInputStream();
Log.debug("got input stream");
}
catch (java.net.MalformedURLException e) {
Log.error("bad URL given: " + uri, e);
}
catch (java.io.FileNotFoundException e) {
Log.error("item at URI " + uri + " not found");
}
catch (java.io.IOException e) {
Log.error("IO problem for " + uri, e);
}
return is;
}
else
{
return super.resolveAndOpenStream(uri);
}
}
private boolean _isProtectedResource(String uri)
{
// does this require authentication?
}
}