Showing posts with label J2SE. Show all posts
Showing posts with label J2SE. Show all posts

Friday, December 9, 2011

Inserting and Retrieving images to sqlite

I tried with BinaryStream and Blob but both don't work properly.
Blob is not implemented with sqlite jdbc driver.

Note : setBytes() , getBytes() works properly with sqlite.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqliteimagetest;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

Showing an Image

file path -> FileInputStream -> BufferedImage -> Image -> ImageIcon

   1 File imageFile=new File("C:\\garden.jpg");
   2 try {
   3     FileInputStream fis=new FileInputStream(imageFile);
   4     
   5     BufferedImage buffImg=ImageIO.read(fis);
   6     Image img=Toolkit.getDefaultToolkit().createImage(buffImg.getSource());
   7     ImageIcon imgico=new ImageIcon(img);
   8     jLabel1.setIcon(imgico);
   9     
  10 } catch (Exception e) {
  11     System.out.println("Error : " + e.getMessage());
  12 }

JLabel only supports ImageIcon type.


JNI | Returning an Array

ArrayRet.java

   1 public class ArrayRet{
   2         public native int[][] rand2DArray(int size);
   3 
   4         public static void main(String args[]){
   5                 ArrayRet jniAr=new ArrayRet();
   6                 System.loadLibrary("randomintdll");
   7                 
   8                 int randArr[][]=jniAr.rand2DArray(3);
   9                 for(int i=0;i<3;i++){
  10                         for(int j=0;j<3;j++){
  11                                 System.out.print(" " + randArr[i][j]);
  12                         }
  13                         System.out.println();
  14                 }
  15                 
  16         }
  17 }

JNI With Arrays

ArrayJNI.java

   1 class ArrayJNI{
   2         public native int getSum(int[] numbers);
   3         
   4         public static void main(String args[]){
   5                 System.loadLibrary("arraylib");
   6                 ArrayJNI arrjni=new ArrayJNI();
   7                 int intarr[]={12,32,43,13,54,5,-2};
   8                 int sum=arrjni.getSum(intarr);
   9                 System.out.println(sum);
  10         }
  11 }

Thursday, December 8, 2011

JNI With Strings

StringJNICaller.java

   1 class StringJNICaller{
   2         public native String getUppercaseString(String inpStr);
   3         
   4         public static void main(String args[]){
   5                 System.loadLibrary("stringlib");
   6                 
   7                 StringJNICaller sjnic=new StringJNICaller();
   8                 String uprstr=sjnic.getUppercaseString("hello sri lanka");
   9                 System.out.println(uprstr);
  10         }
  11 }

My JNI Experiments Part I | Handling integers

JNI(Java Native Interface) is a very important aspect of Java programming language. JNI provides an interface to the native environment. So we can execute native methods within our Java programs. Lets assume that we  have to do a task that need high performance in our Java program. C/C++ is good for high performance tasks rather than Java. If we can cooperate or embed C/C++ with our Java code, we can achieve our high performance targets. But Java is interpreted(byte code based) and C/C++ is compiled. So JNI is the way to cooperate those native code with our Java code.

Lets assume we have calculate factorial of a given integer value. calculation is done by a code written in C++. We call the C++ native method within our Java program and show the result on the console.


Step 1 : write Java code
OurProgram.java
This Java program contains the main method
   1 public class OurProgram{
   2         public static void main(String args[]){
   3                 try{
   4                         System.loadLibrary("myfactorialdll");
   5                         JNICallerInt jnic=new JNICallerInt();
   6                         
   7                         for(int i=1;i<15;i++){
   8                                 int factorial=jnic.factorial(i);
   9                                 System.out.println("Factorial of " + i + " is \t : " + factorial);
  10                         }
  11                         
  12                 }catch(UnsatisfiedLinkError e){
  13                         System.out.println("Unsatisfied link to the dll");
  14                 }
  15         }
  16 }

JNICallerInt.java
create a interface to the C++ method
   1 class JNICallerInt{
   2         public native int factorial(int value);
   3 }

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis