Я использую код JavaFX для разработки приложения Android с использованием openSource JavaFxPorts. У меня есть рабочий фрагмент для отображения данных изображения из сокетов в JavaFX.
Mat frame = new Mat();
buffer = packet.getData();
frame = Imgcodecs.imdecode(new MatOfByte(buffer), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Image imageToShow = Utils.mat2Image(frame);
Utils.onFXThread(mainGui.maincontroller.getImageView().imageProperty(), imageToShow);
Utils.onFXThread( GUIMotionComposer.m_cFrameView.imageProperty(), imageToShow);
Как использовать этот существующий код или какие изменения необходимы для обеспечения совместимости вышеуказанного кода с Android?
файл build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.3.16'
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
}
}
apply plugin: "me.tatarka.retrolambda"
apply plugin: 'org.javafxports.jfxmobile'
retrolambda {
javaVersion JavaVersion.VERSION_1_7
defaultMethods false
incremental true
}
mainClassName = 'org.fxgui.mainGui'
repositories {
jcenter()
}
dependencies {
//compile files('libs/MCLibrary_state.jar','libs/opencv-340.jar')
compile fileTree(dir: 'libs', include: ['*.jar'])
retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:+'
}
sourceSets.main {
java {
srcDir 'src/main/java/org/fxgui'
}
resources {
srcDirs = ['src/main/resources/images', 'src/main/resources']
include "**/*.fxml"
}
}
jfxmobile {
javafxportsVersion = '8.60.9'
android {
compileSdkVersion = '23'
buildToolsVersion = '23.0.3'
androidSdk = 'C:\\Users\\sairam\\androidsdk'
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/jdom-info.xml'
exclude 'LICENSE'
exclude 'NOTICE'
exclude '**'
exclude 'META-INF/services/org.xmlpull.v1.XmlPullParserFactory'
exclude 'META-INF/**'
exclude 'AndroidManifest.xml'
}
}
}
Utils.java
package org.fxgui;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import org.opencv.core.Mat;
//import android.graphics.Bitmap;
//import android.graphics.Bitmap;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
/**
* Provide general purpose methods for handling OpenCV-JavaFX data conversion.
* Moreover, expose some "low level" methods for matching few JavaFX behavior.
*
* @author <a href="mailto:luigi.derussis@polito.it">Luigi De Russis</a>
* @author <a href="http://max-z.de">Maximilian Zuleger</a>
* @version 1.0 (2016-09-17)
* @since 1.0
*
*/
public final class Utils
{
/**
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
*
* @param frame
* the {@link Mat} representing the current frame
* @return the {@link Image} to show
*/
public static Image mat2Image(Mat frame)
{
try
{
return SwingFXUtils.toFXImage(matToBufferedImage(frame), null);
}
catch (Exception e)
{
System.err.println("Cannot convert the Mat obejct: " + e);
return null;
}
}
/**
* Generic method for putting element running on a non-JavaFX thread on the
* JavaFX thread, to properly update the UI
*
* @param property
* a {@link ObjectProperty}
* @param value
* the value to set for the given {@link ObjectProperty}
*/
public static <T> void onFXThread(final ObjectProperty<T> property, final T value)
{
Platform.runLater(() -> {
property.set(value);
});
}
/**
* Support for the {@link mat2image()} method
*
* @param original
* the {@link Mat} object in BGR or grayscale
* @return the corresponding {@link BufferedImage}
*/
private static BufferedImage matToBufferedImage(Mat original)
{
BufferedImage image = null;
int width = original.width(), height = original.height(), channels = original.channels();
byte[] sourcePixels = new byte[width * height * channels];
original.get(0, 0, sourcePixels);
if (original.channels() > 1)
{
image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
}
else
{
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
}
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
return image;
}
}
Спасибо