I have compiled the JH LABS java code into a library (jImageFilters.jar) that can be used in B4J. The attached project uses inline Java code to apply some of the available image filters by making use of the jImageFilters.jar. I have added 7 of the available filters in classes within the B4J project.
Posting the following:
1. B4J project demonstrating the filters that I have added
2. B4J library files - JH LABS code that was compiled into a jar (copy it to your B4J additional library folder
3. The JH LABS Java code as you will need it to reference public getters and setters that should be added to the inline Java code if you want to add some of the other filters that JH LABS provides for (there are 167 odd filters that you can make use of). See the classes that I have added and cross reference them to the JH LABS java code for those classes that I have added in the B4J project via inline Java code.
I will continue to add to this project as and when time permits (....and perhaps convert this into a fully fledged B4J library via a wrapper).
Will gladly assist anyone that needs help should you want to add any of the other available filters.
Sample code for the B4J main module (but also see the code in the B4J classes within the project - incorporating inline Java code):
Example of code in Class Module for the Edge Filter:
Posting the following:
1. B4J project demonstrating the filters that I have added
2. B4J library files - JH LABS code that was compiled into a jar (copy it to your B4J additional library folder
3. The JH LABS Java code as you will need it to reference public getters and setters that should be added to the inline Java code if you want to add some of the other filters that JH LABS provides for (there are 167 odd filters that you can make use of). See the classes that I have added and cross reference them to the JH LABS java code for those classes that I have added in the B4J project via inline Java code.
I will continue to add to this project as and when time permits (....and perhaps convert this into a fully fledged B4J library via a wrapper).
Will gladly assist anyone that needs help should you want to add any of the other available filters.
Sample code for the B4J main module (but also see the code in the B4J classes within the project - incorporating inline Java code):
B4X:
#Region Project Attributes
#MainFormWidth: 1000
#MainFormHeight: 800
#AdditionalJar: sqlite-jdbc-3.7.2
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private ImageView1 As ImageView
Private ImageView2 As ImageView
Private ImageView3 As ImageView
Private ImageView4 As ImageView
Private ImageView5 As ImageView
Private ImageView6 As ImageView
Private ImageView7 As ImageView
Private ImageView8 As ImageView
Private ImageView9 As ImageView
Private ImageView10 As ImageView
Private ImageView11 As ImageView
Private ImageView12 As ImageView
Private b As Image
Private bf As boxBlurFilter
Private ef As embossFilter
Private crystal As crystallizeFilter
Private tf As twirlFilter
Private gsf As grayscaleFilter
Private invf As invertFilter
Private edgef1 As edgeFilter
Private edgef2 As edgeFilter
Private edgef3 As edgeFilter
Private edgef4 As edgeFilter
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
MainForm.RootPane.LoadLayout("main") 'Load the layout file.
MainForm.Show
MainForm.BackColor = fx.Colors.ARGB(100,0,0,255)
' MainForm.Initialize("", -1, -1)
MainForm.WindowWidth = 1500
MainForm.WindowLeft = 0
MainForm.WindowTop = 0
MainForm.WindowHeight = 1000
b.Initialize(File.DirAssets,"Stitch1.jpg")
ImageView1.SetImage(b)
Log(ImageView1)
Log(b)
'Apply a Box Blur Filter
bf.Initialize
bf.HRadius = 0.0
bf.VRadius = 5.0
bf.Iterations = 15
ImageView2.SetImage(bf.applyBoxBlurFilter(b))
'Apply an Emboss Filter
ef.Initialize
ef.BumpHeight = 1.0
' ef.Azimuth = 3.0
' ef.Elevation = 30.0
ef.Emboss = True
ImageView3.SetImage(ef.applyEmbossFilter(b))
'Apply Crystallize Filter
crystal.Initialize
crystal.Scale = 60
crystal.EdgeThickness = 0.4
crystal.FadeEdges = False
crystal.EdgeColor = 0x44ff0000
ImageView4.SetImage(crystal.applyCrystallizeFilter(b))
'Apply Twirl Filter
tf.Initialize
tf.Angle = 2.876
tf.Radius = 500
ImageView5.SetImage(tf.applyTwirlFilter(b))
'Apply Grayscale Filter
gsf.Initialize
ImageView6.SetImage(gsf.applyGrayscaleFilter(b))
'Apply Invert Filter
invf.Initialize
ImageView7.SetImage(invf.applyInvertFilter(b))
'Apply Edge Filter - PREWITT
edgef1.Initialize
edgef1.HEdgeMatrix = edgef1.PREWITT
edgef1.VEdgeMatrix = edgef1.PREWITT
ImageView8.SetImage(edgef1.applyEdgeFilter(b))
'Apply Edge Filter - SOBEL
edgef2.Initialize
edgef2.HEdgeMatrix = edgef2.SOBEL
edgef2.VEdgeMatrix = edgef2.SOBEL
ImageView9.SetImage(edgef2.applyEdgeFilter(b))
'Apply Edge Filter - ROBERTS
edgef3.Initialize
edgef3.HEdgeMatrix = edgef3.ROBERTS
edgef3.VEdgeMatrix = edgef3.ROBERTS
ImageView10.SetImage(edgef3.applyEdgeFilter(b))
'Apply Edge Filter - FREI_CHEN
edgef4.Initialize
edgef4.HEdgeMatrix = edgef4.FREI_CHEN
edgef4.VEdgeMatrix = edgef4.FREI_CHEN
ImageView11.SetImage(edgef4.applyEdgeFilter(b))
'Apply Sobel Edge Filter, then Grayscale Filter, then Invert Filter
ImageView12.SetImage(invf.applyInvertFilter(gsf.applyGrayscaleFilter(edgef2.applyEdgeFilter(b))))
End Sub
Example of code in Class Module for the Edge Filter:
B4X:
'Class module
Sub Class_Globals
Private fx As JFX
Private nativeMe As JavaObject
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
nativeMe = Me
End Sub
public Sub applyEdgeFilter(img As Image) As Image
Return nativeMe.RunMethod("applyEdgeFilter", Array(img))
End Sub
public Sub ROBERTS() As String
Return "ROBERTS"
End Sub
public Sub PREWITT() As String
Return "PREWITT"
End Sub
public Sub SOBEL() As String
Return "SOBEL"
End Sub
public Sub FREI_CHEN() As String
Return "FREI_CHEN"
End Sub
public Sub setVEdgeMatrix(vEdgeMatrix As String)
nativeMe.RunMethod("setVEdgeMatrix", Array(vEdgeMatrix))
End Sub
public Sub setHEdgeMatrix(hEdgeMatrix As String)
nativeMe.RunMethod("setHEdgeMatrix", Array(hEdgeMatrix))
End Sub
#If Java
import java.awt.image.BufferedImage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.awt.Graphics2D;
import javafx.embed.swing.SwingFXUtils;
import com.jhlabs.image.EdgeFilter;
import com.jhlabs.image.ImageMath;
import java.awt.image.*;
import java.awt.*;
import java.awt.geom.*;
private String edgeFilterHtype = "SOBEL_H";
private String edgeFilterVtype = "SOBEL_V";
private float[] vEdgeMatrix = SOBEL_V;
private float[] hEdgeMatrix = SOBEL_H;
private static float R2 = (float)Math.sqrt(2);
public final static float[] ROBERTS_V = {
0, 0, -1,
0, 1, 0,
0, 0, 0,
};
public final static float[] ROBERTS_H = {
-1, 0, 0,
0, 1, 0,
0, 0, 0,
};
public final static float[] PREWITT_V = {
-1, 0, 1,
-1, 0, 1,
-1, 0, 1,
};
public final static float[] PREWITT_H = {
-1, -1, -1,
0, 0, 0,
1, 1, 1,
};
public final static float[] SOBEL_V = {
-1, 0, 1,
-2, 0, 2,
-1, 0, 1,
};
public static float[] SOBEL_H = {
-1, -2, -1,
0, 0, 0,
1, 2, 1,
};
public final static float[] FREI_CHEN_V = {
-1, 0, 1,
-R2, 0, R2,
-1, 0, 1,
};
public static float[] FREI_CHEN_H = {
-1, -R2, -1,
0, 0, 0,
1, R2, 1,
};
public Image applyEdgeFilter(Image img) {
BufferedImage bmp = SwingFXUtils.fromFXImage(img, null);
BufferedImage srcImage = bmp;
EdgeFilter ef = new EdgeFilter();
ef.setVEdgeMatrix(vEdgeMatrix);
ef.setHEdgeMatrix(hEdgeMatrix);
srcImage = ef.filter(srcImage, srcImage);
img = SwingFXUtils.toFXImage(srcImage, null);
return img;
}
public void setVEdgeMatrix(String verticalEdgeMatrix) {
if (verticalEdgeMatrix.equals("ROBERTS"))
this.vEdgeMatrix = ROBERTS_V;
if (verticalEdgeMatrix.equals("PREWITT"))
this.vEdgeMatrix = PREWITT_V;
if (verticalEdgeMatrix.equals("SOBEL"))
this.vEdgeMatrix = SOBEL_V;
if (verticalEdgeMatrix.equals("FREI_CHEN"))
this.vEdgeMatrix = FREI_CHEN_V;
}
public void setHEdgeMatrix(String horizontalEdgeMatrix) {
if (horizontalEdgeMatrix.equals("ROBERTS"))
this.hEdgeMatrix = ROBERTS_H;
if (horizontalEdgeMatrix.equals("PREWITT"))
this.hEdgeMatrix = PREWITT_H;
if (horizontalEdgeMatrix.equals("SOBEL"))
this.hEdgeMatrix = SOBEL_H;
if (horizontalEdgeMatrix.equals("FREI_CHEN"))
this.hEdgeMatrix = FREI_CHEN_H;
}
#End If
Attachments
Last edited: