DSPRelated.com
Forums

Java and Matlab (how to interface the two)

Started by c r March 29, 2003

I am currently working with a package that was put out by the University of Maine that controls a framegrabber via java using mathematica.  However, I need to envoke the same set of java files he is using in matlab.  I need to know how to bring in the java methods and how to access the variables (like he does, using Module etc).  The documentation I have found from mathworks is extremely limited and I was unable to make clear sense of it.  Thanks

Craig Roush

Here is the mathematica code:

(* Title: RIVA : Real-time Image Viewing and Acquisition *)

(* Author: Susan Jarmuz and Mariusz Jankowski.
        E - mail : m...@usm.maine.edu *)
      
(* Summary: This package provides camera connectivity, frame initialization,
        and the functionality of Snap and SnapArea. *)
  
(* Package Version: 2.1 *)

(* Release Notes:
        Ver 0.15 Sep 2001 for JLink 1.1.2
        Ver 0.21 Oct 2001 for JLink 1.1.2
        Ver 0.25 Nov 2001 for JLink 1.1.2
        Ver 1.00 Dec 2001 for JLink 1.1.2 *)
       
(* Mathematica Version: 4.1 *)

(* Copyright: Copyright 2001, Susan Jarmuz and Mariusz Jankowski.
        All rights reserved. *)
             
(****************************************  Package  ****************************************)             
             
BeginPackage["RIVA`"]

<<Jlink`

(*  Usage Section  *)

RIVAInitialize::usage = "RIVAInitialize[] loads the RIVA graphical user interface and initializes its functionality.";
RIVASnap::usage = "RIVASnap[] acquires a live image from the attached camera and returns an array of dimensions 1026-by-1296.
RIVASnap[x, y, dx, dy] returns an array of dimensions dy-by-dx.";
ZoomFactor::usage = "This option for RIVASnap[] sets the x and y direction zoom factors for the camera.  Valid zoom factors are {1,2,4,8}.  Default ZoomFactor -> {1,1}.";
RIVAClose::usage = "RIVAClose[] closes the graphical user interface and deinitializes the camera."
$RIVAFrame::usage = "Internal RIVA variable."

(*-  Code Section  *)

Begin["`Private`"]

Width = 1296;
Height = 1026;

Options[RIVASnap] = ZoomFactor -> {1, 1}
RIVASnap::ZoomFactorErrorMsg = "Incorrect ZoomFactor value.  Allowable values for ZoomFactor are {1,2,4,8}."
RIVASnap::OutOfImageRange= "Selected image sub-area is outside of the zoomed image range.";
   
RIVAInitialize[] := Module[{frame},
      InstallJava[CommandLine -> "javaw"];
%          InstallJava[] launches the Java runtime and prepares it to
%          be used from Mathematica. InstallJava has a number of
%          options to control the name of the Java runtime program,
%          the classpath used, etc. Only one Java runtime is ever
%          launched; subsequent calls to InstallJava after the first
%          have no effect.

      frame = JavaNew["MainFrame", "RIVA V1.0"];
           %Constructor:  In Java the usage would be MyClass obj = new MyClass(args)
                                           
      frame@validate[];
      JavaShow[frame];
      $RIVAFrame = frame;]

RIVASnap[opts___?OptionQ] := JavaBlock[Module[{xzoom, yzoom},
    {xzoom,yzoom} = ZoomFactor /. Flatten[{opts}] /. Options[RIVASnap];
    If[MemberQ[{{1,1}, {1, 2}, {1, 4}, {1, 8}, {2, 1}, {2, 2}, {2, 4}, {2,8}}, {xzoom, yzoom}],
      Reverse[Partition[$RIVAFrame@snapImage[xzoom, yzoom], Width/xzoom]],
      Message[RIVASnap::ZoomFactorErrorMsg]; Return[$Failed]]]]   
     
RIVASnap[x_, y_, dx_, dy_, opts___?OptionQ] := JavaBlock[Module[{xzoom, yzoom},
    {xzoom,yzoom} = ZoomFactor /. Flatten[{opts}] /. Options[RIVASnap];
    If[(x+dx<Width/xzoom)&&(y+dy<Height/yzoom),
    If[MemberQ[{{1,1}, {1, 2}, {1, 4}, {1, 8}, {2, 1}, {2, 2}, {2, 4}, {2,8}}, {xzoom, yzoom}],     
      Reverse[Partition[$RIVAFrame@snapImageArea[x, y, dx, dy, xzoom, yzoom],dx]],
      Message[RIVASnap::ZoomFactorErrorMsg]; Return[$Failed]],
       Message[RIVASnap::OutOfImageRange];Return[$Failed]]]]

RIVAClose[] :=
      JavaBlock[Module[{},
      $RIVAFrame@closeRIVA[];
   $RIVAFrame@dispose[]]]
 
End[]

EndPackage[]

Print["RIVA has been loaded successfully."]