Convert Positive Image to Negative Using OpenCV Library

Maruthi Krishna
Updated on 09-Apr-2020 13:59:35

383 Views

Algorithm to convert an image to negativeGet the red green blue values of each pixelSubtract each color value from 255 and save them as new color values.Create a new pixel value from the modified colors.set the new value to the pixel.Implementation in JavaRead the required image using ImageIO.read() method.Get the height and width of the image.Using nested for loops traverse through each pixel in the image.Get the pixel value using the getRGB() method.Create a Color object bypassing the above-retrieved pixel value as parameter.Get the red, green, blue values from the color object using the getRed(), getGreen() and getBlue() methods respectively.Calculate ... Read More

Check PHP Cookie Existence and Set Its Value

AmitDiwan
Updated on 09-Apr-2020 13:51:39

2K+ Views

Based on the PHP manual, the existence of a cookie can’t be found.A reference from the manual: “Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.”The reason being cookies are response headers to the browser and the browser needs to then send them back along with the next request. This is the reason they are available on the second page load only.But here is a work around for the same: The $_COOKIE can be set when the setcookie function is called −if(!isset($_COOKIE['lg'])) {    setcookie('lg', 'ro');    $_COOKIE['lg'] ... Read More

Access Variables from Parent Scope in Anonymous PHP Function

AmitDiwan
Updated on 09-Apr-2020 13:43:42

619 Views

The ‘use’ keyword can be used to bind variables into the specific function’s scope.Use the use keyword to bind variables into the function's scope −Example Live Demo

Prevent Multiple Inserts When Submitting a Form in PHP

AmitDiwan
Updated on 09-Apr-2020 13:40:58

2K+ Views

PHP session can be used to prevent multiple inserts when submitting a form. PHP session sets a session variable (say $_SESSION['posttimer']) that sets the current timestamp on POST. Before processing the form in PHP, the $_SESSION['posttimer'] variable is checked for its existence and checked for a specific timestamp difference (say 2 or 3 seconds). This way, those insertions that are actually duplicates can be identified and removed.Simple form −// form.html         The reference to ‘my_session_file.php’ in the above will have the below lines of code −Exampleif (isset($_POST) && !empty($_POST)) {    if (isset($_SESSION['posttimer'])) {     ... Read More

When to Use the ServiceLoader Class in Java 9

raja
Updated on 09-Apr-2020 12:22:21

305 Views

Java has a ServiceLoader class from java.util package that can help to locate service providers at the runtime by searching in the classpath. For service providers defined in modules, we can look at the sample application to declare modules with service and how it works.For instance, we have a "test.app" module that we need to use Logger that can be retrieved from System.getLogger() factory method with the help of LoggerFinder service.module com.tutorialspoint.test.app {    requires java.logging;    exports com.tutorialspoint.platformlogging.app;    uses java.lang.System.LoggerFinder; }Below is the test.app.MainApp class:package com.tutorialspoint.platformlogging.app; public class MainApp {    private static Logger LOGGER = System.getLogger();    public static void ... Read More

Casting Variable as Object Type in Foreach Loop in PHP

AmitDiwan
Updated on 09-Apr-2020 11:44:45

1K+ Views

This depends on the IDE that is being used. For example, Netbeans and IntelliJ can enable the usage of @var in a comment −/* @var $variable ClassName */ $variable->This way, the IDE would know that the ‘$variable’ is a class of the ClassName after the hint ‘->’ is encountered.In addition, an @return annotation can be created with a method that specifies that the return type will be an array of ClassName objects. This data can be accessed using a foreach loop that fetches the values of the objects −function get_object_type() {    return $this->values; } foreach( $data_object-> values as $object_attribute ... Read More

Sort PHP Multidimensional Array by Sub Value in PHP

AmitDiwan
Updated on 09-Apr-2020 11:43:24

390 Views

The ‘usort’ function can be used to sort multidimensional arrays in PHP. It sorts an array based on a user-defined criteria −Example Live DemoOutputThis will produce the following output −2 4 63 81An array with 4 elements is declared and this array is passed to the usort function, as well as calling the user-defined ‘my_sort’ function on the elements to make sure that the sorting takes place is ascending order.

Add Object Element to Array in PHP

AmitDiwan
Updated on 09-Apr-2020 11:41:50

3K+ Views

The code is as follows −Example$object = new stdClass(); $object->name = "My name"; $myArray[] = $object;OutputThis will produce the following output −Suppose myArray already contains ‘a’ and ‘c’, the value of “My name” will be added to it. It becomes Array {    a:0, c:1, “My name”:2 }The object is created and then it is pushed to the end of the array (that was previously present).Alternative$myArray[] = (object) ['name' => 'My name'];

Detect Language from String in PHP

AmitDiwan
Updated on 09-Apr-2020 11:40:36

1K+ Views

The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −Examplerequire_once 'Text/LanguageDetect.php'; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) {    echo $result->getMessage(); } else {    print_r($result); }OutputThis will produce the following output −Array (    [german] => 0.407037037037    [dutch] => 0.288065843621    [english] => 0.283333333333    [danish] => 0.234526748971 )It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.

Read Last 5 Lines of a Text File in PHP

AmitDiwan
Updated on 09-Apr-2020 11:39:18

674 Views

To read only 5 last lines of the text file, the code is as follows −Example$file = file("filename.txt"); for ($i = max(0, count($file)-6); $i < count($file); $i++) {    echo $file[$i] . ""; }OutputThis will produce the following output −Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.The file is opened and the number of lines in the file are counted, and beginning from the last line, 5 lines are read.

Advertisements