Related
I'm trying to set the background of a SubScene as an image, but when I do, this happens:
This is the code:
public void start(Stage stage) throws Exception {
PerspectiveCamera camera = new PerspectiveCamera(true);
Group model = new Group(new Box(200, 10, 10));
Group course = new Group(model);
BorderPane borderPane = new BorderPane();
Pane pane1 = new Pane(course);
SubScene subScene1 = new SubScene(pane1, 1000, 720);
Group homeCameraXform = new Group();
model.getChildren().add(homeCameraXform);
homeCameraXform.getChildren().add(camera);
subScene1.setCamera(camera);
camera.setNearClip(.1);
camera.setFarClip(10000);
camera.setTranslateZ(-1000);
Xform s = new Xform();
s.getChildren().add(camera);
subScene1.setOnMouseDragged(e -> s.rx.setAngle(s.rx.getAngle()+ 10));
borderPane.setLeft(subScene1);
subScene1.setFill(new ImagePattern(new Image("file.jpg")));
Scene scene = new Scene(borderPane, 1280,720, true);
stage.setScene(scene);
stage.show();
}
For a normal Scene with a depthBuffer, I just needed to do scene.setFill(new ImagePattern(new Image("file"))). When I try setFill(Color.RED) instead of setFill(new ImagePattern(new Image(""))), it works as expected. What else do I have to do to setFill with an ImagePattern?
Using a skydome as 3d background
This is a skydome :
A 3d mesh usually made for realtime rendering. Its normal are pointing inside to see its texture from inside .
This is a single javafx app with two files you can test.
( you may notice that is the skydome is rotating not the camera . Camera motion is not implemented in this example)
App.java
public class App extends Application {
#Override
public void start(Stage stage) throws IOException {
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateY(-1.3);
camera.setTranslateZ(-1.5);
Shape3D cube = new Box(0.1, 0.1, 0.1);
cube.setTranslateY(-1.3);
Rotate rotate = new Rotate();
rotate.setAxis(Rotate.Y_AXIS);
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(3 * 10),
new KeyValue(rotate.angleProperty(), 360 * 10)));
MeshView skyDome = getSkyDome();
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image("/space.jpg"));
skyDome.setMaterial(material);
Group group3d = new Group(camera, skyDome, cube);
Scene scene = new Scene(group3d, 640, 480, true, SceneAntialiasing.BALANCED);
scene.setCamera(camera);
stage.setScene(scene);
stage.show();
skyDome.getTransforms().setAll(rotate);
timeline.play();
}
public static void main(String[] args) {
launch();
}
private MeshView getSkyDome() throws IOException {
FXMLLoader loader = new FXMLLoader(App.class.getResource("/skydome.fxml"));
loader.load();
return (MeshView) loader.getRoot();
}
}
skidome.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<MeshView id="default">
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<MeshView id="default">
<mesh>
<TriangleMesh>
<points>
-0.094396, -2.432509, 0.474559, 0.094396, -2.432509, 0.474559, 0.0, -2.480165, 0.0, -0.342138, -1.753742, 1.720044, 0.342138, -1.753742, 1.720044, 0.268816, -2.062182, 1.35143, -0.268815, -2.062182, 1.351428, -0.402311, -1.377906, 2.022556, 0.402312, -1.377906, 2.022558, -0.447025, -0.949118, 2.247345, 0.447025, -0.949118, 2.247346, -0.474558, -0.483856, 2.385768, 0.474559, -0.483856, 2.385769, 0.268816, -2.432509, 0.402312, 0.185164, -2.291374, 0.930881, 0.527302, -2.291374, 0.789163, -0.185164, -2.291374, 0.930881, 0.789163, -2.291374, 0.527302, 0.402312, -2.432509, 0.268816, 0.765523, -2.062182, 1.145687, 0.974327, -1.753742, 1.458183, 1.145687, -1.377906, 1.714641, 1.273019, -0.949118, 1.905208, 1.35143, -0.483856, 2.022558, 0.483856, 0.0, 2.432509, 1.377906, 0.0, 2.062182, 0.474559, -2.432509, 0.094396, 0.402312, -2.432509, -0.268816, 0.268816, -2.432509, -0.402312, 1.145687, -2.062182, 0.765523, 1.458183, -1.753742, 0.974327, 1.714641, -1.377906, 1.145687, 1.905207, -0.949118, 1.273019, 2.022558, -0.483856, 1.35143, 2.062182, 0.0, 1.377905, 1.35143, -2.062182, 0.268816, 0.930881, -2.291374, 0.185164, 1.720044, -1.753742, 0.342138, 2.022557, -1.377906, 0.402312, 2.247345, -0.949118, 0.447025, 2.385769, -0.483856, 0.474559, 2.432509, 0.0, 0.483856, 0.789163, -2.291374, -0.527302, 0.527302, -2.291374, -0.789163, 1.35143, -2.062182, -0.268816, 0.930881, -2.291374, -0.185164, 1.720044, -1.753742, -0.342138, 2.022557, -1.377906, -0.402312, 2.247345, -0.949118, -0.447025, 2.385769, -0.483856, -0.474559, 2.432509, 0.0, -0.483856, 0.474559, -2.432509, -0.094396, 1.458183, -1.753742, -0.974326, 1.145687, -2.062182, -0.765523, 1.714641, -1.377906, -1.145687, 1.905207, -0.949118, -1.273019, 2.022557, -0.483856, -1.35143, 2.062181, 0.0, -1.377906, -0.483855, 0.0, 2.432507, 0.974326, -1.753742, -1.458183, 0.765523, -2.062182, -1.145687, 1.145686, -1.377906, -1.714641, 1.273019, -0.949118, -1.905207, 1.351429, -0.483856, -2.022557, 1.377905, 0.0, -2.062181, 0.185164, -2.291374, -0.930881, 0.094396, -2.432509, -0.474559, 0.402311, -1.377906, -2.022557, 0.342138, -1.753742, -1.720044, 0.447025, -0.949118, -2.247345, 0.474559, -0.483856, -2.385769, 0.483855, 0.0, -2.432508, -0.185164, -2.291374, -0.930881, -0.094396, -2.432509, -0.474559, 0.268816, -2.062182, -1.351429, -0.447025, -0.949118, -2.247345, -0.402312, -1.377906, -2.022557, -0.474559, -0.483856, -2.385769, -0.483857, 0.0, -2.432508, -0.527302, -2.291374, -0.789163, -0.268816, -2.432509, -0.402311, -0.268816, -2.062182, -1.351429, -0.342138, -1.753742, -1.720044, -1.273019, -0.949118, -1.905207, -1.145687, -1.377906, -1.714641, -1.35143, -0.483856, -2.022557, -1.377906, 0.0, -2.06218, -0.789163, -2.291374, -0.527301, -0.402311, -2.432509, -0.268816, -0.765523, -2.062182, -1.145686, -0.974326, -1.753742, -1.458183, -2.022557, -0.483856, -1.351429, -1.905207, -0.949118, -1.273019, -2.062181, 0.0, -1.377904, -0.930881, -2.291374, -0.185164, -0.474559, -2.432509, -0.094396, -1.145686, -2.062182, -0.765523, -1.458183, -1.753742, -0.974326, -1.714641, -1.377906, -1.145686, -2.385769, -0.483856, -0.474558, -2.247345, -0.949118, -0.447025, -2.432508, 0.0, -0.483855, -0.930881, -2.291374, 0.185164, -0.474559, -2.432509, 0.094396, -1.351429, -2.062182, -0.268816, -1.720044, -1.753742, -0.342138, -2.022557, -1.377906, -0.402311, -2.432507, 0.0, 0.483857, -2.385768, -0.483856, 0.47456, -0.789162, -2.291374, 0.527302, -0.402311, -2.432509, 0.268816, -1.351429, -2.062182, 0.268816, -1.720044, -1.753742, 0.342138, -2.022557, -1.377906, 0.402312, -2.247345, -0.949118, 0.447025, -0.527301, -2.291374, 0.789163, -0.268816, -2.432509, 0.402311, -1.145686, -2.062182, 0.765523, -1.458182, -1.753742, 0.974326, -1.71464, -1.377906, 1.145687, -1.905207, -0.949118, 1.273019, -2.022556, -0.483856, 1.35143, -2.062179, 0.0, 1.377906, -0.765522, -2.062182, 1.145686, -0.974326, -1.753742, 1.458183, -1.145686, -1.377906, 1.714641, -1.273018, -0.949118, 1.905207, -1.351429, -0.483856, 2.022557, -1.377903, 0.0, 2.06218
</points>
<texCoords>
0.474147, 0.554411, 0.459173, 0.542211, 0.497469, 0.510688, 0.401617, 0.69249904, 0.339297, 0.642836, 0.381025, 0.607478, 0.426818, 0.644186, 0.374268, 0.745052, 0.294258, 0.681365, 0.344075, 0.803576, 0.244831, 0.72359, 0.310262, 0.86816204, 0.188605, 0.77230597, 0.450007, 0.52519, 0.420663, 0.57419896, 0.402144, 0.54010296, 0.450793, 0.598541, 0.398004, 0.501432, 0.448031, 0.505932, 0.352741, 0.55584896, 0.300594, 0.57283497, 0.244277, 0.59156096, 0.182, 0.612661, 0.111153, 0.637178, 0.123258, 0.82906103, 0.027992, 0.666368, 0.453578, 0.48735303, 0.482963, 0.463072, 0.502329, 0.46113402, 0.346159, 0.49715102, 0.291204, 0.49302697, 0.231585, 0.488998, 0.165286, 0.484999, 0.089498, 0.480954, 1.0E-4, 0.47660702, 0.362536, 0.440125, 0.408982, 0.464001, 0.313039, 0.415165, 0.259036, 0.38845903, 0.198627, 0.359159, 0.129173, 0.32611, 0.046856, 0.28750902, 0.468006, 0.41481602, 0.507085, 0.41079903, 0.399699, 0.393515, 0.43354, 0.43354303, 0.363408, 0.351206, 0.323555, 0.30540198, 0.278658, 0.25451702, 0.226602, 0.196352, 0.164136, 0.127671, 0.465839, 0.47228998, 0.434986, 0.311454, 0.452158, 0.364707, 0.415973, 0.253478, 0.394455, 0.188712, 0.369528, 0.11443597, 0.339115, 0.022300005, 0.268737, 0.947176, 0.51661, 0.30255198, 0.511808, 0.35838902, 0.521646, 0.24165797, 0.527191, 0.17353898, 0.533629, 0.09482598, 0.542525, 0.0026819706, 0.544709, 0.422158, 0.520958, 0.466788, 0.623624, 0.271716, 0.595337, 0.32599097, 0.655278, 0.21057498, 0.691454, 0.14150602, 0.733968, 0.058188975, 0.57503, 0.44712198, 0.535978, 0.47915798, 0.569293, 0.375632, 0.758412, 0.295969, 0.705741, 0.33930302, 0.819598, 0.24593401, 0.893654, 0.18697703, 0.593374, 0.481776, 0.545089, 0.49633002, 0.615601, 0.41373003, 0.658739, 0.378128, 0.819903, 0.41351098, 0.754973, 0.432998, 0.895401, 0.392321, 0.984045, 0.368082, 0.596988, 0.52072, 0.546915, 0.51565504, 0.643545, 0.46662802, 0.696888, 0.45051098, 0.90723, 0.554314, 0.831046, 0.544786, 0.9999, 0.565755, 0.585435, 0.557967, 0.541211, 0.534176, 0.648957, 0.525985, 0.704147, 0.531596, 0.764159, 0.537763, 0.85742, 0.706935, 0.791173, 0.669206, 0.934559, 0.751607, 0.560597, 0.58788097, 0.52888, 0.549083, 0.631263, 0.582636, 0.679851, 0.608819, 0.732569, 0.637317, 0.810515, 0.896712, 0.75453, 0.82741296, 0.526317, 0.606017, 0.511816, 0.55813897, 0.59343, 0.628034, 0.628208, 0.670555, 0.665895, 0.716673, 0.707717, 0.76798403, 0.487793, 0.609733, 0.492608, 0.56000197, 0.541351, 0.655504, 0.557295, 0.707849, 0.574561, 0.76454103, 0.593638, 0.827516, 0.614138, 0.899719, 0.637505, 0.984956, 0.482914, 0.66112, 0.47785, 0.715472, 0.47246, 0.774325, 0.466571, 0.839563, 0.45879, 0.913558, 0.450524, 0.9999
</texCoords>
<faces>
0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 3, 3, 5, 5, 6, 6, 7, 7, 8, 8, 4, 4, 7, 7, 4, 4, 3, 3, 9, 9, 10, 10, 8, 8, 9, 9, 8, 8, 7, 7, 11, 11, 12, 12, 10, 10, 11, 11, 10, 10, 9, 9, 1, 1, 13, 13, 2, 2, 14, 14, 15, 15, 13, 13, 14, 14, 13, 13, 1, 1, 6, 6, 5, 5, 14, 14, 6, 6, 14, 14, 16, 16, 15, 15, 17, 17, 18, 18, 15, 15, 18, 18, 13, 13, 13, 13, 18, 18, 2, 2, 5, 5, 19, 19, 15, 15, 5, 5, 15, 15, 14, 14, 4, 4, 20, 20, 19, 19, 4, 4, 19, 19, 5, 5, 8, 8, 21, 21, 20, 20, 8, 8, 20, 20, 4, 4, 10, 10, 22, 22, 21, 21, 10, 10, 21, 21, 8, 8, 12, 12, 23, 23, 22, 22, 12, 12, 22, 22, 10, 10, 24, 24, 25, 25, 23, 23, 24, 24, 23, 23, 12, 12, 18, 18, 26, 26, 2, 2, 27, 27, 28, 28, 2, 2, 19, 19, 29, 29, 17, 17, 19, 19, 17, 17, 15, 15, 20, 20, 30, 30, 29, 29, 20, 20, 29, 29, 19, 19, 21, 21, 31, 31, 30, 30, 21, 21, 30, 30, 20, 20, 22, 22, 32, 32, 31, 31, 22, 22, 31, 31, 21, 21, 23, 23, 33, 33, 32, 32, 23, 23, 32, 32, 22, 22, 25, 25, 34, 34, 33, 33, 25, 25, 33, 33, 23, 23, 29, 29, 35, 35, 36, 36, 29, 29, 36, 36, 17, 17, 30, 30, 37, 37, 35, 35, 30, 30, 35, 35, 29, 29, 31, 31, 38, 38, 37, 37, 31, 31, 37, 37, 30, 30, 32, 32, 39, 39, 38, 38, 32, 32, 38, 38, 31, 31, 33, 33, 40, 40, 39, 39, 33, 33, 39, 39, 32, 32, 34, 34, 41, 41, 40, 40, 34, 34, 40, 40, 33, 33, 42, 42, 43, 43, 28, 28, 42, 42, 28, 28, 27, 27, 17, 17, 36, 36, 26, 26, 17, 17, 26, 26, 18, 18, 35, 35, 44, 44, 45, 45, 35, 35, 45, 45, 36, 36, 37, 37, 46, 46, 44, 44, 37, 37, 44, 44, 35, 35, 38, 38, 47, 47, 46, 46, 38, 38, 46, 46, 37, 37, 39, 39, 48, 48, 47, 47, 39, 39, 47, 47, 38, 38, 40, 40, 49, 49, 48, 48, 40, 40, 48, 48, 39, 39, 41, 41, 50, 50, 49, 49, 41, 41, 49, 49, 40, 40, 26, 26, 51, 51, 2, 2, 45, 45, 42, 42, 27, 27, 45, 45, 27, 27, 51, 51, 46, 46, 52, 52, 53, 53, 46, 46, 53, 53, 44, 44, 47, 47, 54, 54, 52, 52, 47, 47, 52, 52, 46, 46, 48, 48, 55, 55, 54, 54, 48, 48, 54, 54, 47, 47, 49, 49, 56, 56, 55, 55, 49, 49, 55, 55, 48, 48, 50, 50, 57, 57, 56, 56, 50, 50, 56, 56, 49, 49, 51, 51, 27, 27, 2, 2, 58, 58, 24, 24, 12, 12, 58, 58, 12, 12, 11, 11, 44, 44, 53, 53, 42, 42, 44, 44, 42, 42, 45, 45, 52, 52, 59, 59, 60, 60, 52, 52, 60, 60, 53, 53, 54, 54, 61, 61, 59, 59, 54, 54, 59, 59, 52, 52, 55, 55, 62, 62, 61, 61, 55, 55, 61, 61, 54, 54, 56, 56, 63, 63, 62, 62, 56, 56, 62, 62, 55, 55, 57, 57, 64, 64, 63, 63, 57, 57, 63, 63, 56, 56, 43, 43, 65, 65, 66, 66, 43, 43, 66, 66, 28, 28, 28, 28, 66, 66, 2, 2, 53, 53, 60, 60, 43, 43, 53, 53, 43, 43, 42, 42, 61, 61, 67, 67, 68, 68, 61, 61, 68, 68, 59, 59, 62, 62, 69, 69, 67, 67, 62, 62, 67, 67, 61, 61, 63, 63, 70, 70, 69, 69, 63, 63, 69, 69, 62, 62, 64, 64, 71, 71, 70, 70, 64, 64, 70, 70, 63, 63, 65, 65, 72, 72, 73, 73, 65, 65, 73, 73, 66, 66, 66, 66, 73, 73, 2, 2, 60, 60, 74, 74, 65, 65, 60, 60, 65, 65, 43, 43, 59, 59, 68, 68, 74, 74, 59, 59, 74, 74, 60, 60, 69, 69, 75, 75, 76, 76, 69, 69, 76, 76, 67, 67, 70, 70, 77, 77, 75, 75, 70, 70, 75, 75, 69, 69, 71, 71, 78, 78, 77, 77, 71, 71, 77, 77, 70, 70, 72, 72, 79, 79, 80, 80, 72, 72, 80, 80, 73, 73, 73, 73, 80, 80, 2, 2, 74, 74, 81, 81, 72, 72, 74, 74, 72, 72, 65, 65, 68, 68, 82, 82, 81, 81, 68, 68, 81, 81, 74, 74, 67, 67, 76, 76, 82, 82, 67, 67, 82, 82, 68, 68, 75, 75, 83, 83, 84, 84, 75, 75, 84, 84, 76, 76, 77, 77, 85, 85, 83, 83, 77, 77, 83, 83, 75, 75, 78, 78, 86, 86, 85, 85, 78, 78, 85, 85, 77, 77, 79, 79, 87, 87, 88, 88, 79, 79, 88, 88, 80, 80, 80, 80, 88, 88, 2, 2, 81, 81, 89, 89, 79, 79, 81, 81, 79, 79, 72, 72, 82, 82, 90, 90, 89, 89, 82, 82, 89, 89, 81, 81, 76, 76, 84, 84, 90, 90, 76, 76, 90, 90, 82, 82, 85, 85, 91, 91, 92, 92, 85, 85, 92, 92, 83, 83, 86, 86, 93, 93, 91, 91, 86, 86, 91, 91, 85, 85, 87, 87, 94, 94, 95, 95, 87, 87, 95, 95, 88, 88, 88, 88, 95, 95, 2, 2, 89, 89, 96, 96, 87, 87, 89, 89, 87, 87, 79, 79, 90, 90, 97, 97, 96, 96, 90, 90, 96, 96, 89, 89, 84, 84, 98, 98, 97, 97, 84, 84, 97, 97, 90, 90, 83, 83, 92, 92, 98, 98, 83, 83, 98, 98, 84, 84, 91, 91, 99, 99, 100, 100, 91, 91, 100, 100, 92, 92, 93, 93, 101, 101, 99, 99, 93, 93, 99, 99, 91, 91, 94, 94, 102, 102, 103, 103, 94, 94, 103, 103, 95, 95, 95, 95, 103, 103, 2, 2, 96, 96, 104, 104, 94, 94, 96, 96, 94, 94, 87, 87, 97, 97, 105, 105, 104, 104, 97, 97, 104, 104, 96, 96, 98, 98, 106, 106, 105, 105, 98, 98, 105, 105, 97, 97, 92, 92, 100, 100, 106, 106, 92, 92, 106, 106, 98, 98, 101, 101, 107, 107, 108, 108, 101, 101, 108, 108, 99, 99, 102, 102, 109, 109, 110, 110, 102, 102, 110, 110, 103, 103, 103, 103, 110, 110, 2, 2, 104, 104, 111, 111, 102, 102, 104, 104, 102, 102, 94, 94, 105, 105, 112, 112, 111, 111, 105, 105, 111, 111, 104, 104, 106, 106, 113, 113, 112, 112, 106, 106, 112, 112, 105, 105, 100, 100, 114, 114, 113, 113, 100, 100, 113, 113, 106, 106, 99, 99, 108, 108, 114, 114, 99, 99, 114, 114, 100, 100, 109, 109, 115, 115, 116, 116, 109, 109, 116, 116, 110, 110, 110, 110, 116, 116, 2, 2, 111, 111, 117, 117, 109, 109, 111, 111, 109, 109, 102, 102, 112, 112, 118, 118, 117, 117, 112, 112, 117, 117, 111, 111, 113, 113, 119, 119, 118, 118, 113, 113, 118, 118, 112, 112, 114, 114, 120, 120, 119, 119, 114, 114, 119, 119, 113, 113, 108, 108, 121, 121, 120, 120, 108, 108, 120, 120, 114, 114, 107, 107, 122, 122, 121, 121, 107, 107, 121, 121, 108, 108, 115, 115, 16, 16, 0, 0, 115, 115, 0, 0, 116, 116, 116, 116, 0, 0, 2, 2, 117, 117, 123, 123, 115, 115, 117, 117, 115, 115, 109, 109, 118, 118, 124, 124, 123, 123, 118, 118, 123, 123, 117, 117, 119, 119, 125, 125, 124, 124, 119, 119, 124, 124, 118, 118, 120, 120, 126, 126, 125, 125, 120, 120, 125, 125, 119, 119, 121, 121, 127, 127, 126, 126, 121, 121, 126, 126, 120, 120, 122, 122, 128, 128, 127, 127, 122, 122, 127, 127, 121, 121, 123, 123, 6, 6, 16, 16, 123, 123, 16, 16, 115, 115, 124, 124, 3, 3, 6, 6, 124, 124, 6, 6, 123, 123, 125, 125, 7, 7, 3, 3, 125, 125, 3, 3, 124, 124, 126, 126, 9, 9, 7, 7, 126, 126, 7, 7, 125, 125, 127, 127, 11, 11, 9, 9, 127, 127, 9, 9, 126, 126, 128, 128, 58, 58, 11, 11, 128, 128, 11, 11, 127, 127, 16, 16, 14, 14, 1, 1, 16, 16, 1, 1, 0, 0, 36, 36, 45, 45, 51, 51, 36, 36, 51, 51, 26, 26
</faces>
<faceSmoothingGroups>
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
</faceSmoothingGroups>
</TriangleMesh>
</mesh>
</MeshView>
space.jpg
Result
I'm trying to set up an MLKit detection project on Android using Tensorflow library, I have got false values on the output using Tensorflow lite ( different values than Frozen model inference ).
I doubt that the problem is with the input ( image ), So I want to compare the two same images that I have.
To do this task, I have used PIL image and numpy libraries on python to get bytes arrays, and I have converted the drawable image to bitmap and from bitmap to bytes arrays.
I don't know if the function np.asarray should give the same value as those two functions below:
Java code:
private float[][][][] bitmapToInputArray() {
// [START mlkit_bitmap_input]
Bitmap bitmap= getYourInputImage();
int batchNum = 0;
float[][][][] input = new float[1][112][112][3];
for (int x = 0; x < 112; x++) {
for (int y = 0; y < 112; y++) {
int pixel = bitmap.getPixel(x, y);
// Normalize channel values to [-1.0, 1.0]. This requirement varies by
// model. For example, some models might require values to be normalized
// to the range [0.0, 1.0] instead.
input[batchNum][x][y][0] = (Color.red(pixel))/ 255.0f;
input[batchNum][x][y][1] = (Color.green(pixel)) / 255.0f;
input[batchNum][x][y][2] = (Color.blue(pixel))/ 255.0f;
Log.i("Input","input"+input[batchNum][x][y][0]);
Log.i("input","input"+input[batchNum][x][y][1]);
}
}
// [END mlkit_bitmap_input]
return input;
}
public byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream = null;
try {
stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
Log.e(ThemedSpinnerAdapter.Helper.class.getSimpleName(), "ByteArrayOutputStream was not closed");
}
}
}
}
private Bitmap getYourInputImage() {
// This method is just for show
BitmapDrawable drawable = (BitmapDrawable) image2.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap bitmapp=Bitmap.createScaledBitmap(bitmap,112,112,true);
Bitmap bitmap2= bitmapp.copy(Bitmap.Config.ARGB_8888, true);
return bitmap2;
}
byte[] bytes=convertBitmapToByteArray(bitmap1);
Log.i("byte",""+ Arrays.toString(bytes));
float[][][][] inp = new float[1][112][112][3];
inp=bitmapToInputArray();
Log.i("byte2",""+Arrays.toString(inp[0]));
Python script:
img = Image.open("irisdata-300VW_Dataset_2015_12_14-017-000880.jpg")
img.load()
img = img.resize((112, 112), PIL.Image.ANTIALIAS)
print(str(image_to_byte_array(img)))
# Normalize to [0, 1]
data = np.asarray( img, dtype="float32")
print(data)
Output of java code:
2019-08-20 19:01:13.589 1513-1513/com.example.irisdetection I/byte: [-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, -1, -37, 0, 67, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -37, 0, 67, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -64, 0, 17, 8, 0, 112, 0, 112, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 29, 0, 0, 2, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 6, 9, 4, 5, 10, 3, 2, 1, -1, -60, 0, 69, 16, 0, 1, 3, 3, 1, 5, 5, 4, 7, 5, 6, 5, 5, 0, 0, 0, 3, 1, 2, 4, 5, 6, 17, 33, 0, 7, 18, 19, 49, 8, 34, 65, 81, 97, 20, 35, 113, -127, 9, 21, 50, 51, -111, -95, -79, 36, 66, -63, -47, -16, 22, 23, 67, 82, 83, -31, 37, 52, 98, 114, -15, 99, 115, -126, -125, -94, -1, -60, 0, 28, 1, 0, 2, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 4, 5, 2, 3, 8, 0, 1, -1, -60, 0, 55, 17, 0, 0, 4, 3, 6, 3, 8, 1, 3, 4, 3, 1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 17, 33, 0, 6, 49, 65, 81, 97, 18, 19, 113, 7, 20, 35, -127, -111, -95, -79, -16, -47, 34, -63, -31, 21, 36, 67, -15, 8, 22, 50, 51, -1, -38, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, -28, -105, 115, 116, 26, 45, 98, -97, 117, 74, -71, -31, -57, -88, 40, -73, 93, 88, -83, 83, 89, 39, 41, -20, -43, 40, -7, 28, 115, 12, -100, -52, -116, -88, 78, 53, -45, 84, -49, -53, 102, 114, 124, 34, -52, -20, -3, -72, 91, -115, 6, -58, -120, -76, -5, -102, -126, 71, 36, -107, 43, -97, -11, 61, 93, -17, 78, 96, 63, 115, -35, -52, 8, -3, -30, -89, -40, -26, 117, -40, 29, -70, 107, 106, 21, 112, 86, -36, 7, 73, 36, 120, -9, 30, -19, -18, -48, -56, 86, -79, -88, -31, -106, -105, 36, -53, -53, 103, 30, -92, 121, 24, -120, 76, 47, -7, -15, -99, 54, 103, 44, -88, -107, 42, -57, 98, 107, 90, 83, -98, 105, 20, -21, 43, 126, 53, -117, 106, 58, -71, -116, 84, -114, 58, -11, 22, 53, 77, -84, 119, 47, -34, -14, -52, 72, 102, 127, -68, -57, -61, 77, -78, 126, -35, 18, -79, 112, 9, 38, 41, 40, 50, 3, 104, 63, -7, -64, 66, 97, -100, -66, -115, -115, 59, 43, -119, 21, 43, -50, -111, 79, 62, 82, -55, -72, 64, 70, -76, -92, -6, 5, 119, -54, -42, 15, -40, -126, -36, -81, 86, 109, 8, 55, 69, -62, 114, 31, -39, -30, 125, 65, 65, 9, 120, -47, -111, -24, -48, 36, -55, 56, -58, 49, -12, 103, 52, -14, 77, -24, -68, 12, -41, 24, -38, -49, 45, -6, 122, -80, -104, -106, -88, 32, -76, 92, 72, -67, 51, -13, 95, 92, -2, 105, -90, 112, -85, -1, 0, 101, -21, 110, -105, 72, -35, 117, -93, 21, -22, -48, 56, 84, 56, 50, 9, -62, -33, -74, 105, 35, -25, -99, 126, 42, 71, -89, -126, -86, -2, 91, 77, 119, -59, -67, 56, 86, 101, 29, -50, -116, -41, 28, -83, 78, 1, 2, 59, 85, 78, 103, -82, -120, -120, -119, -16, 84, -8, -81, -82, -43, -119, 2, 73, -74, -32, 34, 124, -95, -91, 103, -120, -56, 48, -108, -3, 55, 10, -128, 72, 44, 54, -11, 68, -43, -117, 63, 85, 41, 75, -67, -81, -115, 104, 11, -128, 98, 30, 84, -99, -90, 117, -103, -47, 13, 32, -111, 99, 59, 8, -30, 120, -85, 59, -38, -8, 42, 116, -50, -67, 51, -25, -116, 109, -17, 6, -128, 85, 123, 72, 34, 46, 60, 28, -72, -8, 39, 69, -49, -97, -13, -38, -77, -28, -17, 107, 125, -75, -103, -113, -97, 108, 110, -34, -19, -88, 0, 69, -53, 80, 84, -87, 74, -114, 69, 34, -82, -124, 81, -94, 97, 124, -4, -70, 109, 40, -94, 118, -112, -19, 67, 109, 76, -114, -54, -2, -25, 36, -118, -104, -30, 35, -122, -39, -111, -90, 10, 79, 39, 61, 73, -53, 69, -41, 25, 93, 116, -12, -38, -87, 70, -4, -59, 84, -83, 100, 30, 67, 47, -65, 103, 51, 120, 107, -94, 36, 68, -124, -63, -31, -56, 39, 44, -58, -127, -112, -53, 110, -125, -106, 118, 97, 30, -117, 46, 58, 100, -68, 46, 110, 123, -51, -41, -89, 84, -4, -4, -68, 126, 91, 123, -44, -30, 3, -39, -107, 80, 92, 92, 89, -18, -89, -34, 105, -29, -4, 83, -11, -38, 47, -70, -19, -17, 10, -4, -89, 8, -43, 90, 49, 40, -14, -36, 1, -72, -111, -56, -113, 86, 101, 90, -103, 31, 125, 62, -13, -29, -32, -102, -90, -60, -55, -43, 90, 28, 126, 23, 56, 106, -118, -115, -30, 114, 47, -106, 5
2019-08-20 19:01:13.590 1513-1513/com.example.irisdetection I/byte2: [[[F#8bc81c1, [[F#4123d66, [[F#d5d68a7, [[F#ed2a154, [[F#7ce78fd, [[F#c74c9f2, [[F#66de843, [[F#627ec0, [[F#50ea7f9, [[F#201933e, [[F#57cc59f, [[F#250c6ec, [[F#f88cab5, [[F#7ffa54a, [[F#d721cbb, [[F#1f965d8,
Output of python code:
How can I get the same output of the python code with Java code using bitmap as an input?
I'm writing a quick sort algorithm in GWT, between each iteration I'm trying to print the current status.
This is the method
private int partition(int[] list_of_numbers, int first, int last) {
int pivot = list_of_numbers[first];
int up = first;
int down = last;
do {
while ((up < last) && pivot >= list_of_numbers[up]) {
up++;
}
while (pivot < list_of_numbers[down]) {
down--;
}
if (up < down) {
swap(list_of_numbers, up, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
}
} while (up < down);
swap(list_of_numbers, first, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
return down;
}
It works really well and we can see from the logger that it puts out the following
30, 3, 23, 7, 77, 46, 62, 91, 89, 22, 48, 96, 32, 40, 95,
30, 3, 23, 7, 22, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 23, 7, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 7, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
7, 3, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 40, 91, 89, 77, 48, 96, 32, 62, 95,
3, 7, 22, 23, 30, 46, 40, 32, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 62, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 77, 48, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 48, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 95, 96,
but on the output to the screen it will only display the last iteration
I would like to put a delay of 1 second after each sort where the user can see the current array.
I'm not sure how to use the timer in this case. As well I don't know if it is changing the value each time so fast that I only see the last one or if it's only changing it at the end.
A quick google search yields a page guiding you through delaying calculations and updating the interface.
You can find it here.
I found this website where you try drawing a perfect circle. Just for fun I coded this small Java application, that draws a circle from the middle of your screen:
// here is my massive array
private static int[] circle = {0, -100, 1, -100, 2, -100, 3, -100, 4, -100, 5, -100, 6, -100, 7, -100, 8, -100, 9, -100, 10, -100, 11, -99, 12, -99, 13, -99, 14, -99, 15, -99, 16, -99, 17, -98, 18, -98, 19, -98, 20, -98, 21, -98, 22, -97, 23, -97, 24, -97, 25, -97, 26, -96, 27, -96, 28, -96, 29, -96, 30, -95, 31, -95, 32, -95, 33, -94, 34, -94, 35, -94, 36, -93, 37, -93, 38, -92, 39, -92, 40, -92, 41, -91, 42, -91, 43, -90, 44, -90, 45, -89, 46, -89, 47, -88, 48, -88, 49, -87, 50, -86, 51, -86, 52, -85, 53, -85, 54, -84, 55, -83, 56, -84, 57, -83, 58, -82, 59, -82, 60, -81, 61, -80, 62, -79, 63, -79, 64, -78, 65, -77, 66, -76, 67, -75, 68, -74, 69, -73, 70, -72, 71, -71, 72, -70, 73, -69, 74, -68, 75, -67, 76, -66, 77, -65, 78, -64, 79, -63, 79, -62, 80, -61, 81, -60, 82, -59, 82, -58, 83, -57, 84, -56, 83, -55, 84, -54, 85, -53, 85, -52, 86, -51, 86, -50, 87, -49, 88, -48, 88, -47, 89, -46, 89, -45, 90, -44, 90, -43, 91, -42, 91, -41, 92, -40, 92, -39, 92, -38, 93, -37, 93, -36, 94, -35, 94, -34, 94, -33, 95, -32, 95, -31, 95, -30, 96, -29, 96, -28, 96, -27, 96, -26, 97, -25, 97, -24, 97, -23, 97, -22, 98, -21, 98, -20, 98, -19, 98, -18, 98, -17, 99, -16, 99, -15, 99, -14, 99, -13, 99, -12, 99, -11, 100, -10, 100, -9, 100, -8, 100, -7, 100, -6, 100, -5, 100, -4, 100, -3, 100, -2, 100, -1, 100, 0, 100, 1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 6, 100, 7, 100, 8, 100, 9, 100, 10, 100, 11, 99, 12, 99, 13, 99, 14, 99, 15, 99, 16, 99, 17, 98, 18, 98, 19, 98, 20, 98, 21, 98, 22, 97, 23, 97, 24, 97, 25, 97, 26, 96, 27, 96, 28, 96, 29, 96, 30, 95, 31, 95, 32, 95, 33, 94, 34, 94, 35, 94, 36, 93, 37, 93, 38, 92, 39, 92, 40, 92, 41, 91, 42, 91, 43, 90, 44, 90, 45, 89, 46, 89, 47, 88, 48, 88, 49, 87, 50, 86, 51, 86, 52, 85, 53, 85, 54, 84, 55, 83, 56, 84, 57, 83, 58, 82, 59, 82, 60, 81, 61, 80, 62, 79, 63, 79, 64, 78, 65, 77, 66, 76, 67, 75, 68, 74, 69, 73, 70, 72, 71, 71, 72, 70, 73, 69, 74, 68, 75, 67, 76, 66, 77, 65, 78, 64, 79, 63, 79, 62, 80, 61, 81, 60, 82, 59, 82, 58, 83, 57, 84, 56, 83, 55, 84, 54, 85, 53, 85, 52, 86, 51, 86, 50, 87, 49, 88, 48, 88, 47, 89, 46, 89, 45, 90, 44, 90, 43, 91, 42, 91, 41, 92, 40, 92, 39, 92, 38, 93, 37, 93, 36, 94, 35, 94, 34, 94, 33, 95, 32, 95, 31, 95, 30, 96, 29, 96, 28, 96, 27, 96, 26, 97, 25, 97, 24, 97, 23, 97, 22, 98, 21, 98, 20, 98, 19, 98, 18, 98, 17, 99, 16, 99, 15, 99, 14, 99, 13, 99, 12, 99, 11, 100, 10, 100, 9, 100, 8, 100, 7, 100, 6, 100, 5, 100, 4, 100, 3, 100, 2, 100, 1, 100, 0,100,-1,100,-2,100,-3,100,-4,100,-5,100,-6,100,-7,100,-8,100,-9,100,-10,100,-11,99,-12,99,-13,99,-14,99,-15,99,-16,99,-17,98,-18,98,-19,98,-20,98,-21,98,-22,97,-23,97,-24,97,-25,97,-26,96,-27,96,-28,96,-29,96,-30,95,-31,95,-32,95,-33,94,-34,94,-35,94,-36,93,-37,93,-38,92,-39,92,-40,92,-41,91,-42,91,-43,90,-44,90,-45,89,-46,89,-47,88,-48,88,-49,87,-50,86,-51,86,-52,85,-53,85,-54,84,-55,83,-56,84,-57,83,-58,82,-59,82,-60,81,-61,80,-62,79,-63,79,-64,78,-65,77,-66,76,-67,75,-68,74,-69,73,-70,72,-71,71,-72,70,-73,69,-74,68,-75,67,-76,66,-77,65,-78,64,-79,63,-79,62,-80,61,-81,60,-82,59,-82,58,-83,57,-84,56,-83,55,-84,54,-85,53,-85,52,-86,51,-86,50,-87,49,-88,48,-88,47,-89,46,-89,45,-90,44,-90,43,-91,42,-91,41,-92,40,-92,39,-92,38,-93,37,-93,36,-94,35,-94,34,-94,33,-95,32,-95,31,-95,30,-96,29,-96,28,-96,27,-96,26,-97,25,-97,24,-97,23,-97,22,-98,21,-98,20,-98,19,-98,18,-98,17,-99,16,-99,15,-99,14,-99,13,-99,12,-99,11,-100,10,-100,9,-100,8,-100,7,-100,6,-100,5,-100,4,-100,3,-100,2,-100,1,-100,0,-100,-1,-100,-2,-100,-3,-100,-4,-100,-5,-100,-6,-100,-7,-100,-8,-100,-9,-100,-10,-100,-11,-99,-12,-99,-13,-99,-14,-99,-15,-99,-16,-99,-17,-98,-18,-98,-19,-98,-20,-98,-21,-98,-22,-97,-23,-97,-24,-97,-25,-97,-26,-96,-27,-96,-28,-96,-29,-96,-30,-95,-31,-95,-32,-95,-33,-94,-34,-94,-35,-94,-36,-93,-37,-93,-38,-92,-39,-92,-40,-92,-41,-91,-42,-91,-43,-90,-44,-90,-45,-89,-46,-89,-47,-88,-48,-88,-49,-87,-50,-86,-51,-86,-52,-85,-53,-85,-54,-84,-55,-83,-56,-84,-57,-83,-58,-82,-59,-82,-60,-81,-61,-80,-62,-79,-63,-79,-64,-78,-65,-77,-66,-76,-67,-75,-68,-74,-69,-73,-70,-72,-71,-71,-72,-70,-73,-69,-74,-68,-75,-67,-76,-66,-77,-65,-78,-64,-79,-63,-79,-62,-80,-61,-81,-60,-82,-59,-82,-58,-83,-57,-84,-56,-83,-55,-84,-54,-85,-53,-85,-52,-86,-51,-86,-50,-87,-49,-88,-48,-88,-47,-89,-46,-89,-45,-90,-44,-90,-43,-91,-42,-91,-41,-92,-40,-92,-39,-92,-38,-93,-37,-93,-36,-94,-35,-94,-34,-94,-33,-95,-32,-95,-31,-95,-30,-96,-29,-96,-28,-96,-27,-96,-26,-97,-25,-97,-24,-97,-23,-97,-22,-98,-21,-98,-20,-98,-19,-98,-18,-98,-17,-99,-16,-99,-15,-99,-14,-99,-13,-99,-12,-99,-11,-100,-10,-100,-9,-100,-8,-100,-7,-100,-6,-100,-5,-100,-4,-100,-3,-100,-2,-100,-1,-100,0};
public static void main(String[] args) {
try {
// Waiting for the website to load
Thread.sleep(3000);
} catch (InterruptedException e) {}
// Getting the center of the screen
Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
// Drawing the circle by clicking on each point in the array relative to the middle of the screen
for (int x = 0; x < circle.length; x+=2) {
// Calculating the x and y coordinates
int xx = p.x + circle[x];
int yy = p.y + circle[x+1];
click(xx, yy);
// waiting 10ms so the website can calculate the action
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
public static void click(int x, int y) {
try {
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (Exception e) {}
}
The application is made to be used on this website. But my problem is as you can see, that I have to save each point of the circle in an array and that array is very big. If I wanted to expand the radius (100px) I would need to rewrite all that points. Is there a easier way of doing so? Maybe by using an equation or something like that.
You can calculate the coordinates of a point on the circumference of a circle for a given angle (in radians) by doing {cos(angle), sin(angle)}.
So one way we can get a list of points is to loop around and get the coordinates for every n degrees, where n is configurable (you'll need more points for a larger radius to draw a smooth circle).
I'd first declare a Point class to hold these values:
class Point
{
final int x;
final int y;
Point(final double x, final double y)
{
this.x = (int) x; // we're dealing with pixels, so just truncate it
this.y = (int) y;
}
#Override
public String toString()
{
return "{" + x + ", " + y + "}";
}
}
Then we can do the following to populate an array of Points:
final int NUM_POINTS = 1000;
final double RADIUS = 100d;
final Point[] points = new Point[NUM_POINTS];
for (int i = 0; i < NUM_POINTS; ++i)
{
final double angle = Math.toRadians(((double) i / NUM_POINTS) * 360d);
points[i] = new Point(
Math.cos(angle) * RADIUS,
Math.sin(angle) * RADIUS
);
}
System.out.println(Arrays.toString(points));
You can use Circle Equations to draw a nearly perfect circle. You can not draw a perfect circle because of your screen resolution.
You only need x and y coordinates.
public Point [] generateCoordinates(Point center, int diameter) {
//You will use algorithm here
}
Here is an example algorithm;
https://www.tutorialspoint.com/computer_graphics/circle_generation_algorithm.htm
https://www.varsitytutors.com/hotmath/hotmath_help/topics/equation-of-a-circle
you should use this equation, so you can adjust circle size and precision
I used this code to generate my array but its very slow, thats why the answer by #Michael is better...
int r = Integer.parseInt(rad) / 2;
String[] circle = {"0", "0"};
String[] circleq = {"0", "0"};
String[] circlew = {"0", "0"};
String[] circlee = {"0", "0"};
for (int x = 0; x < r; x++) {
double yy;
yy = Math.sqrt((r*r) - (x*x));
int y = (int) Math.round(yy);;
circle = add(circle, Integer.parseInt("-" + x));
circle = add(circle, Integer.parseInt("-" + y));
}
String[] circle2 = circle;
for (int c = 0; c < circle2.length; c++) {
if ((c & 1) == 0 || c == 0) {
circleq = add(circleq, Integer.parseInt(circle2[c]));
} else {
circleq = add(circleq, Integer.parseInt(circle2[c].replace("-", "")));
}
}
for (int c = 0; c < circle2.length; c++) {
circlew = add(circlew, Integer.parseInt(circle2[c].replace("-", "")));
}
for (int c = 0; c < circle2.length; c++) {
if ((c & 1) == 0 || c == 0) {
circlee = add(circlee, Integer.parseInt(circle2[c].replace("-", "")));
} else {
circlee = add(circlee, Integer.parseInt(circle2[c]));
}
}
It saves each quarter of the circle in a seperate array.
I have a byte[] that I want to convert to an Image and display the image in a label.
The byte[] is of a jpeg 2000 format.
I have tried the code below but it returns null:
InputStream in = new ByteArrayInputStream(bytearray);
BufferedImage image = ImageIO.read(in);
The image value comes back as null.
I want to be able to display the image in a label like below:
jLabel.setIcon(new ImageIcon(image));
Thanks
To convert an array of bytes, i.e. byte[] into an Image, use getImage(). Probably the easiest way to do this is to instantiate an ImageIcon using the ImageIcon(byte[]) constructor, and then call getImage(). This is illustrated in the method below, particularly the last line:
public Image createImage(){
//ccurve.png
byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
0, 73, 69, 78, 68, -82, 66, 96, -126};
return new ImageIcon(b).getImage();
}
I think this can by used for png, gif, bmp, and jpg images. Also the byte array does not have to be hard-coded, as in this example.
If you want an ImageIcon instead of an Image, don't call getImage():
public ImageIcon createImageIcon(){
//ccurve.png
byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
0, 73, 69, 78, 68, -82, 66, 96, -126};
return new ImageIcon(b);
}
Then you can call jlabel.setIcon(createIconImage());.
ServletOutputStream out = response.getOutputStream();
out.write(user.getBytes());
The above is how its worked for me in the past where user has a profile picture simply stored in a byte array. The servlet realizes this and outputs the image.