Found 9150 Articles for Object Oriented Programming

How to save the current JShell session in Java 9?

raja
Updated on 08-Apr-2020 16:05:07

802 Views

Java 9 has introduced a new feature is the creation of a REPL (Read-Evaluate-Print-Loop) called JShell. It is a command-line prompt tool to evaluate Java code without the need to write a complete program.When we can enter code or internal commands in JShell, we need to use it during the current session. When we can close JShell and log-in again, all of the code previously entered has lost. An internal command has been implemented in order to save all code entered into a file using the "/save" command./ save [file-path] / save -all [file-path] / save -history [file-path] / save -start ... Read More

How to Set/modify the pixels(RGB values) of an image using Java OpenCV Library?

Maruthi Krishna
Updated on 08-Apr-2020 14:14:13

4K+ Views

A pixel is the smallest element of a digital image, each pixel contains the values of alpha, red, green, blue values. The pixel value(s) is stored in a 32-bit memory space holding ARGB values (8 bits each) in the same order. Therefore, to modify the color of an image −You need to loop through each pixel in the image.Get the pixel value using the getRGB() method.Retrieve the ARGB values from the pixel value.Perform the required modifications to the RGB values.Create a new pixel value with the modified RGB values.Set the new pixel value(s) using the setRGB() method.Setting the ARGB values ... Read More

How to perform Bitwise OR operation on two images using Java OpenCV?

Maruthi Krishna
Updated on 08-Apr-2020 14:12:14

337 Views

You can compute bitwise or between two images using the bitwise_or() method of the org.opencv.core.Core class.This method accepts three Mat objects representing the source, destination and result matrices, calculates the bitwise disjunction of each element in the source matrices and stores the result in the destination matrix.ExampleIn the following Java example, we are converting an image into a binary and grayscale and calculating the bitwise disjunction of the results.import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class BitwiseORExample {    public static void main(String args[]) throws Exception {       //Loading the OpenCV core library     ... Read More

How to list all the Collections in a MongoDB database using Java?

Maruthi Krishna
Updated on 08-Apr-2020 14:11:06

3K+ Views

You can print a list of all the existing collections in a database using show collections.ExampleAssume we have created 3 collections in a MongoDB database as shown below −> use sampleDatabase switched to db sampleDatabase > db.createCollection("students") { "ok" : 1 } > db.createCollection("teachers") { "ok" : 1 } > db.createCollection("sample") { "ok" : 1 }Following query lists all the collections in the database −> use sampleDatabase switched to db sampleDatabase > show collections sample students teachersUsing Java programIn Java, you can get the names of all collections in the current database using the listCollectionNames() method of the com.mongodb.client.MongoCollection interface.Therefore ... Read More

Reference to a constructor using method references in Java8

Maruthi Krishna
Updated on 08-Apr-2020 14:09:28

789 Views

Lambda expressions In Java allows you to pass functionality as an argument to a method. You can also call an existing method using lambda expressions.list.forEach(n -> System.out.println(n));Method references are simple, easy-to-read lambda expressions to call/refer and the existing method by name in a lambda expression. In addition to the instance and static methods, you can also refer a constructor by using the new keyword.SyntaxFollowing is the syntax to reference a constructor in Java.ClassName::newExampleinterface myInterface{    Test greet(String data); } class Test{    Test(String data){          System.out.println(data);    } } public class MethodReferences {    public static void ... Read More

How to convert a colored image to blue/green/red image using Java OpenCV library?

Maruthi Krishna
Updated on 08-Apr-2020 14:06:04

401 Views

The cvtColor() method of the Imgproc class changes/converts the color of the image from one to another. This method accepts three parameters −src − A Matrix object representing source.dst − A Matrix object representing the destination.code − An integer value representing the color of the destination image.To convert a colored image to grayscale you need to pass Imgproc.COLOR_RGB2BGR as the third parameter to this method.Exampleimport org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ColorToGrayscale {    public static void main(String args[]) throws Exception {       //Loading the OpenCV core library       System.loadLibrary( Core.NATIVE_LIBRARY_NAME );   ... Read More

How to convert a colored image to grayscale using Java OpenCV library?

Maruthi Krishna
Updated on 08-Apr-2020 14:03:11

2K+ Views

The cvtColor() method of the Imgproc class changes/converts the color of the image from one to another. This method accepts three parameters −src − A Matrix object representing source.dst − A Matrix object representing the destination.code − An integer value representing the color of the destination image.To convert a colored image to grayscale you need to pass Imgproc.COLOR_RGB2GRAY as the third parameter to this method.Exampleimport org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ColorToGrayscale {    public static void main(String args[]) throws Exception {       //Loading the OpenCV core library       System.loadLibrary( Core.NATIVE_LIBRARY_NAME );   ... Read More

Reading a colored image as grey scale using Java OpenCV library.

Maruthi Krishna
Updated on 08-Apr-2020 14:00:57

355 Views

The imread() method of the Imgcodecs class accepts a string value representing a file name as a parameter. This method reads the contents of the specified file into a matrix object and returns it. Using this method you can read the contents of an image.In addition to this, the Imgcodecs class also provides another variant of this method which accepts an integer value representing a flag specifying the required reading mode.The following are the various fields of the Imgcodecs class that can be used as flag values.IMREAD_COLOR − If the flag is set to this value, the loaded image will be ... Read More

How to write an image using Java OpenCV library?

Maruthi Krishna
Updated on 08-Apr-2020 13:53:05

718 Views

Using the OpenCV library you can perform image processing operations such as image filtering, geometrical image transformations, color space conversion, histograms, etc.Writing an imageWhenever you read the contents of an image using the imread() method of the Imgcodecs class the result is read into the Matrix object.You can write/save an image using the imwrite() method. This accepts two parameters namely −File − A string value representing the file path to which the result should be stored.Img − A matrix object containing the data of the image to be saved.ExampleThe following Java example reads the contents of the image cat.jpg as a ... Read More

What is an unnamed module in Java 9?

raja
Updated on 08-Apr-2020 11:52:20

975 Views

An unnamed module is a concept of the unnamed package. It is a module in which packages or classes can't be defined in any named module but exist in the jar file from classpath. If our code can try to load type from those files, the module system attempts to lookup classpath and loads it.An unnamed module read all other modules, including all of the named,  built-in platform modules, and also exports all of its packages. The package in an unnamed module can be ignored, which is also defined in the named module.The unnamed module has access to:All packages exported by all other modules available in module-path.All the jars ... Read More

Advertisements