It is better to have a brief knowledge of the Swing class of java as it is applied in the implementation to do so as whenever there arises a need to deal with images or dealing with the two-dimensional matrix as an output or usage of it in generating the output in java, Swing class comes into play in achieving the same.
We will be using Netbeans IDE o create GUI applications since it is a well crafted open-source IDE that provides features of a component inspector, Drag-and-Drop of widgets, Debugger, Object browser, etc and there is no need to import JAR files unlikely as seen in other IDEs such as an eclipse. Now this gives this IDE an extra edge over the other IDEs.
Basically, this program is divided into 2 parts. One is the design part which is done using swing controls. And the other part is some code part to capture image.
- In the design part, I have used swing controls to design the interface. We just need to drag and drop the controls from palette. Here 2 labels and 1 button are used. The first label name is ‘lblclose‘. There I have set a PNG image and written a code on events-> action performed. When you click on that image the output window will close. For this, I have used dispose() method. The second label name is ‘lblphoto’. Here you can see your image when you run the program. The webcam will open when you run your code. Here I have used one button named ‘btnclick‘. You can see a button CLICK. When you click on the button the image will be captured in your ‘lblphoto‘ label.
- In the code part, I have used Webcam be named it ‘wc’ ie created a webcam object. The library allows you to use your build-in or external webcam directly from Java. It’s designed to abstract commonly used camera features and support multiple capturing frameworks. Then by open() function, the webcam will open. Then after clicking on the CLICK button you will capture an image. Then convert this image according to the size of the label. Then assign this image to label. Then create and start the thread.
- Creation of a new java application and further creating a file under the project.`
- Start dragging toolkit widgets as per need from the palette situated on top-right.
- Click anywhere on the panel area and go to properties to change the background.
- Now double-click on the background area and select any color of choice and press the Ok button.
- Now start dragging widgets on the drawing area.
- Start writing the java program as explained below.
- Select the JAR files as in libraries JAR files need to be imported.
Step 1(a): Create a new Java application by clicking on ‘New Project → Java → Java Application‘ and give a suitable project name. Considering a random example for illustration purposes ‘MyFirstFrame’ and click Finish.
Step 1(b): To create a ‘New File’ under the same Java project ‘MyFirstFrame’, right-click on the project name on the left-hand side of the window, click as below shown, and click finish. E.g. MyFrame.java
New -> JFrame Form and give a suitable file name
Step 2: Now from the palette situated at the right-hand side of the window, start dragging the toolkit widgets as per requirements. To change the background color of the frame, we need to first insert a JPanel and change its properties.
Step 3: Click anywhere on the panel area, go to ‘properties → background.’
Step 4: Double-click on the background option and select any color of the desired choice meeting requirement choice and click OK.
Step 5: After setting the background color, drag other widgets onto the design area. Here I have dragged a button and a label. Button named CLICK and label is used to capture an image. In addition, a border is given to the label.
Step 6(a): Now write the code by right-clicking as shown below
MyFrame.java → Split → Horizontally
Step 6(b): Then this pop-up window will appear. Here click on ‘source‘ to write the code, and further one can click on ‘design‘ to move to design.
Step 7(a): Adding up the jar files, go to Libraries. Right-click on ‘libraries‘ and select ‘ADD JAR/FOLDER‘.
Step 7(b): Select the 3 jar files, then click on ‘open‘. The requirements are as follows as in libraries JAR files need to be imported more specifically 3 JAR files need to be import
The sample input image is as shown below:
This is a snapshot captured from front camera where the code is compiled and run. It will differ with realtime basic what comes in front of front camera when the above same code is compiled and run again.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.