WebCam Video in Browser

Simple html to  stream webcam source in browser(getUserMedia supported browser). Supported Opera browser link is: http://snapshot.opera.com/labs/camera-and-pages

Copy the below code in html file and open that page in getUserMedia supported browsers.

sample.xhtml

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Web Cam Test In Browser</title>
<meta name="generator" content="editplus">
<meta name="author" content="Sreemanth Pulagam">
<script type="text/javascript" src="js/app.js"></script>
</head>
<body>
<video id="myvideo"  autoplay controls>
<video>
<script type="application/javascript">
var myVideo = $('myvideo');

if(navigator.getUserMedia){
console.log('getUserMedia supported in your browser');
navigator.getUserMedia('video',successCallback,errorCallback);
function successCallback(stream){
console.log(stream);
alert(stream);
alert(myVideo);
myVideo.src= stream;
}
function errorCallback(error){
console.log(error);
alert(error);
}

}else{
console.warn('getUserMedia not supported in your browser');
}

</script>
</body>
</html>

app.js Code

function $(id){
	return document.getElementById(id);
}
Posted in HTML5 | Leave a comment

Java Platform Debug Architecture Debug Example

package com.sreerama.debugging;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.sun.jdi.Bootstrap;
import com.sun.jdi.IncompatibleThreadStateException;
import com.sun.jdi.Method;
import com.sun.jdi.ObjectReference;
import com.sun.jdi.StackFrame;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.connect.AttachingConnector;
import com.sun.jdi.connect.Connector;
import com.sun.jdi.connect.Connector.Argument;
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
import com.sun.jdi.event.Event;
import com.sun.jdi.event.EventQueue;
import com.sun.jdi.event.EventSet;
import com.sun.jdi.event.MethodEntryEvent;
import com.sun.jdi.request.EventRequestManager;
import com.sun.jdi.request.MethodEntryRequest;

public class JPDADebugger {
	private static final int SOCKET_ATTACH = 1;
	private VirtualMachine virtualMachine ;

	public void execute(String attachAddress, Vector includeClasses) {
		int index = attachAddress.indexOf(":");
		String hostName = attachAddress.substring(0, index);
		String port = attachAddress.substring(index + 1);
		if (includeClasses == null) {
			includeClasses = new Vector();
		}
		String connectorName = "com.sun.jdi.SocketAttach";
		int connectorType = SOCKET_ATTACH;

		Connector connector = null;
		for (Connector connector2 : Bootstrap.virtualMachineManager()
				.allConnectors()) {
			if (connector2.name().equals(connectorName)) {
				connector = connector2;
			}
		}
		Map arguments = connector.defaultArguments();
		Argument host = arguments.get("hostname");
		host.setValue(hostName);
		Argument portArg = arguments.get("port");
		portArg.setValue(port);

		AttachingConnector attachingConnector = (AttachingConnector) connector;
		try {
			virtualMachine = attachingConnector.attach(arguments);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalConnectorArgumentsException e) {
			e.printStackTrace();
		}

		// / Event request Manager
		EventRequestManager eventRequestManager = virtualMachine
				.eventRequestManager();
		for (Enumeration patterns = includeClasses.elements(); patterns
				.hasMoreElements();) {
			MethodEntryRequest methodEntryRequest = eventRequestManager
					.createMethodEntryRequest();
			String pattern = patterns.nextElement();
			methodEntryRequest.addClassFilter(pattern);
			methodEntryRequest.enable();
		}

		EventQueue eventQueue = virtualMachine.eventQueue();
		boolean running = true;
		while (running) {
			EventSet eventSet = null;
			try {
				eventSet = eventQueue.remove();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for (Event event : eventSet) {
				if (event instanceof MethodEntryEvent) {
					processMethodEntryEvent((MethodEntryEvent) event);
				}
			}
			virtualMachine.resume();
		}

	}

	private void processMethodEntryEvent(MethodEntryEvent event) {
		Method method = event.method();
		String methodNameString = method.name();
		ThreadReference thread = event.thread();
		try {
			List stackFrames = thread.frames();
			int level = 0;
			long calleId = 0;
			String calleClass = null;
			long callerId = 0;
			String callerClass = null;

			for (StackFrame stackFrame : stackFrames) {
				ObjectReference objectReference = stackFrame.thisObject();

				if (level == 0) {
					calleId = objectReference.uniqueID();
					calleClass = objectReference.referenceType().toString();
				}
				if (level == 1) {
					callerId = objectReference.uniqueID();
					callerClass = objectReference.referenceType().toString();
				}
				level++;
				if (level > 1) {
					break;
				}
			}
			System.out.println("" + callerId + ":"
					+ callerClass + "" + methodNameString
					+ "" + calleId + ":" + calleClass
					+ "" + thread.name()
					+ "");

		} catch (IncompatibleThreadStateException e) {
			e.printStackTrace();
		}
	}
}

Test Program

package com.sreerama.debugging;

import java.util.Vector;

public class JPDADebuggerTest {

	public static void main(String[] args) {
		JPDADebugger debugger = new JPDADebugger();
		Vector classes = new Vector();
		classes.add("com.sree.erp.*");
		debugger.execute("localhost:8124", classes);
	}
}


Changes in tomcat catalina.bat file

set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=8124,suspend=n

add bold text need to add for catalina.bat file (%TOMCAT_HOME%/bin/catalina.bat)

Posted in JPDA Example | Leave a comment

Hibernate Exception org.hibernate.exception.SQLGrammarException: ORA-00926: missing VALUES keyword

Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into ITEM (NAME, PRICE, ID) values (?, ?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)
Hibernate: insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)

Exception in thread “main” org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1261)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.sreerama.hibernateexamples.test.ItemTest.main(ItemTest.java:32)
Caused by: java.sql.BatchUpdateException: ORA-00926: missing VALUES keyword

Here Issue is ORA-00926:

Actual issue is  sql query has error –>insert into ITEM-IMAGE (ITEM_ID, IMAGE_NAME) values (?, ?)

Table name should not have ITEM-IMAGE, can be ITEM_IMAGE

*. hbm.xml file

wrong one

<set name=”images” table=“ITEM-IMAGE” lazy=”false”>
<key column=”ITEM_ID”></key>
<element column=”IMAGE_NAME” type=”string” not-null=”false”></element>
</set>

correct one is below.

<set name=”images” table=“ITEM_IMAGE” lazy=”false”>
<key column=”ITEM_ID”></key>
<element column=”IMAGE_NAME” type=”string” not-null=”false”></element>
</set>

Posted in Hibernate | Leave a comment

Finding sum of positive numbers and negative numbers from a sql table

Oracle 

Using decode:

select sum(decode(sign(blocks),1,blocks,0)) as positive_sum , sum(decode(sign(blocks),-1,blocks,0)) as negative_sum from sysfiles

Using case:

select  sum( case when blocks > 0 then blocks else 0 end )as credit  , sum (case when blocks < 0 then blocks else 0 end) as debit from sysfiles

Posted in sql | Leave a comment

Program to find no of Occurances of word.(Simple way)

Code to find the no of occurrences of specific word

package com.sree.file;

public class SpecificWordCountFinder {

	public static void main(String[] args) {
		// find String
		String toFindString = "Java";
		// Content to Find
		String content = "This is very simple tricky Java program ."
				+ "Java is OOP.Java is both compile and interpreted. Java built in multithreaded";

		/**
		 * formula to find occurences = (total length - after replace all * with find string content length)/ findstring length
		 */
		long totalLength = content.length();
		long inputStringLength = toFindString.length();

		String output = content.replaceAll(toFindString, "");
		long outputLength = output.length();
		System.out.println("No of occurances of word" + toFindString + "is :"
				+ (totalLength - outputLength) / inputStringLength);
	}
}
Posted in java | Leave a comment

Welcome to Infocrate

This site is dedicated to java related articles.

Posted in Uncategorized | Leave a comment

simple JNI (Java Native Interface) example

HelloWorld.java

public class HelloWorld
{
private native void print();
public static void main(String[] args)
{
HelloWorld hw = new Helloworld();
hw.print();
}
static
{
System.loadLibrary("HelloWorld");
}
}

1) compile the HelloWorld.java
javac HelloWorld.java

2) create a native method Header file
javah -jni HelloWorld
after this command a file named HelloWorld.h is created in the folder, don’t change that file

3) write a native method implementaiton(HelloWorld.c)
HelloWorld.c

#include
#include
#include "HelloWorld.h"

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("HelloWorld...!\n");
return 0;
}

4) compile the C source
cl -I”c:\program files\java\java1.7\include” -I”c:\program files\java\java1.7\include\win32″ -LD Helloworld.c -FeHelloWorld.dll

5) now run the java program
java HelloWorld

Posted in java | Leave a comment