Camera Application
Hello guys, Today, I am going to show you how to create a completely working camera application in Jakarta EE Stack. I am going to use PrimeFaces (JSF framework) and NetBeans IDE to create the application.
Let's start
Create a Jakarta EE maven project add PrimeFaces dependency
Then create a jsf file(.xhtml).Then copy code of PhotoCam component
<p:card>
<h:form>
<h:panelGrid columns="3" cellpadding="5">
<p:photoCam widgetVar="pc" listener="#{photoCamView.oncapture}" update="photo"/>
<p:commandButton type="button" value="Capture" onclick="PF('pc').capture()"/>
<p:outputPanel id="photo">
<p:graphicImage name="demo/images/photocam/#{photoCamView.filename}.jpeg"
rendered="#{not empty photoCamView.filename}"/>
</p:outputPanel>
</h:panelGrid>
</h:form>
</p:card>
Note : You need to import xmlns:p="http://primefaces.org/ui"
dependency.
Then you need to create CDI
bean for the jsf
file. Name the file as PhotoCamView.java
. Add following code to the file.
@Named
@ViewScoped
public class PhotoCamView implements Serializable {
private String filename;
private String getRandomImageName() {
int i = (int) (Math.random() * 10000000);
return String.valueOf(i);
}
public String getFilename() {
return filename;
}
public void oncapture(CaptureEvent captureEvent) {
filename = getRandomImageName();
byte[] data = captureEvent.getData();
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
//root dirs
String root=externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo"
+ File.separator + "images" + File.separator + "photocam";
File rootFolder=new File(root);
if(!rootFolder.exists()){
try {
Files.createDirectories(Path.of(root));
} catch (IOException ex) {
Logger.getLogger(PhotoCamView.class.getName()).log(Level.SEVERE, null, ex);
}
}
// file
String newFileName = externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo"
+ File.separator + "images" + File.separator + "photocam" + File.separator + filename + ".jpeg";
if(!new File(filename).exists()){
try {
new File(filename).createNewFile();
} catch (IOException ex) {
Logger.getLogger(PhotoCamView.class.getName()).log(Level.SEVERE, null, ex);
}
}
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(newFileName));
imageOutput.write(data, 0, data.length);
imageOutput.close();
} catch (IOException e) {
throw new FacesException("Error in writing captured image.", e);
}
}
}
If you are copying code from the PrimeFaces website then you will see that this code is note working. Because the folder
and file
does not exists. You need to write following code to make your code work.
String root=externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo"
+ File.separator + "images" + File.separator + "photocam";
File rootFolder=new File(root);
if(!rootFolder.exists()){
try {
Files.createDirectories(Path.of(root));
} catch (IOException ex) {
Logger.getLogger(PhotoCamView.class.getName()).log(Level.SEVERE, null, ex);
}
}
// file
String newFileName = externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo"
+ File.separator + "images" + File.separator + "photocam" + File.separator + filename + ".jpeg";
if(!new File(filename).exists()){
try {
new File(filename).createNewFile();
} catch (IOException ex) {
Logger.getLogger(PhotoCamView.class.getName()).log(Level.SEVERE, null, ex);
}
}
Try to run the application allow browser to open your camera and click on capture. One jpeg
file would be saved in your system.
Note : Image files are not saved in project source folder. It is saved in target folder where you build code remains.
Thank you.
Top comments (0)