How can I convert image to arrays using Java code? - java

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?

Related

JavaFX SubScene setFill

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

Input to large for RSA Cipher

I have generated the following Public Key in Android.
fun createCipher(): Cipher
{
val posKey = posPublicKey
posKey.publicKey.modulus
var spec = RSAPublicKeySpec(BigInteger(posPublicKey.publicKey.modulus), BigInteger(posPublicKey.publicKey.exponent))
var fact = KeyFactory.getInstance(KeyProperties.KEY_ALGORITHM_RSA)
publicKey = fact.generatePublic(spec)
var cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
return cipher
}
The RSA key size that I have selected is 4096 bytes
The exponent is 3 bytes and the modulus is 512 bytes
The Modulus Byte Array is as follows:
[-32, -28, -121, 32, 109, -82, 43, 115, 43, -117, 20, 35, 122, 33, -2, 23, 23, -22, 75, 0, 91, 10, -89, 48, 33, -89, 57, 1, 57, -13, 9, -127, 90, 121, -96, -94, 106, -16, -105, -112, -74, 30, -12, 74, -74, 104, -26, 15, 99, -22, -55, -75, 14, -45, 56, 20, 85, 90, 83, -50, 68, -114, 5, -10, -109, 79, 44, 81, 68, -98, -45, -51, -97, 71, 90, -13, -78, 118, -21, -47, 66, 104, -83, 56, -72, -27, 45, 16, 70, 32, -76, -125, -11, 108, 126, -61, -126, 16, 6, -49, -106, -114, 18, 49, 121, -39, -109, 115, 111, -128, 83, 8, -110, -10, -4, 51, -67, 49, 66, 103, -76, -88, -110, 122, 56, 29, -101, 22, 3, 117, -104, -54, -64, -71, 23, 58, 87, 37, 96, 25, -114, 38, 1, -126, 33, -91, -4, 89, -28, 10, 95, -104, -24, -38, 17, 47, -122, 24, -89, 123, 100, 12, -10, -57, -44, 45, 25, 39, -80, -101, -6, -99, -95, -5, 70, 32, 37, -57, -52, -47, -66, 85, 10, -48, 75, 4, -114, 104, -7, -112, -128, 4, 114, 77, -40, 96, 66, 83, -54, 10, 111, 102, -39, -63, 2, -75, 38, 36, 24, 13, -51, 96, 89, -60, -40, 99, 65, 123, 52, -114, 122, 75, 32, -121, 80, -76, -11, -1, -31, -118, -51, -21, 13, 109, 111, -102, 120, -56, 62, -19, -79, 86, -41, -81, 67, -80, -63, 37, 35, 47, 109, -32, 47, -128, 95, -48, -53, -1, -125, -19, -9, -10, 15, -116, -50, 53, -86, -102, -24, 107, 122, -43, -125, 51, 14, 101, 67, 57, 116, 97, -40, -98, -82, -118, -83, 120, -107, -14, 19, -49, -27, 10, 25, 40, 43, -27, 31, 59, -57, 58, 33, -98, 1, -45, -118, 76, -21, -13, -123, 67, 42, -37, -96, -32, 33, 124, 1, 44, -99, 74, 18, 32, 10, -107, -121, 86, -115, -70, -107, 109, 17, -92, 109, -47, 60, -49, -91, 7, -125, 47, 78, 86, 81, -2, -35, 17, 124, 94, -26, -80, -84, 120, 110, 38, -55, -90, -11, 107, 73, 71, 44, 69, -58, 56, -59, 2, 94, 27, 88, 29, -57, 95, -99, 5, 102, -66, 118, -82, 126, 20, -104, -95, 47, -2, 77, -33, 89, -66, -92, 121, -5, 78, 68, -1, -82, -95, -121, 117, -29, 70, 11, -72, 54, -99, -13, -87, 9, 77, -113, 51, -124, -56, -8, 126, -114, -31, 90, -125, -11, 41, -85, 74, 3, 90, -95, 85, 121, 61, 14, 116, 51, -40, -57, -124, -69, -51, -76, -119, -80, 95, 95, 17, -34, 80, -36, 66, -51, 14, -69, -113, 35, -109, -115, -16, -3, -118, 114, -20, -81, 57, -65, 40, -8, -67, -85, 110, 50, -128, 44, -78, 93, -44, -93, 89, -76, 13, 98, -38, -55, -120, 11, 127, 84, -2, 101, 57, -121, -111, 91, -102, -118, 85, -124, -90, 91, -84, 28, 120, -28, -105, 88, -73, 6, 89, 33, 8, 9, 30, 9, -6, 17, 25]
The Exponent Byte Array is as Follows:
[1, 0, 1]
The test Key is as follows:
val stringKey = "8D-F7-5B-15-0F-2A-E5-3E-FD-44-5A-63-50-AC-62-D6-06-2D-59-5C-F1-C3-9A-DB-45-25-0D-7A-72-AE-DF-87"
val stringIV = "FA-94-FD-74-2E-AC-2C-90-79-98-AF-A3-D7-12-5D-A2"
var aeskey = AesKeyBuilder()
val key = (stringKey.replace("-", "")).toByteArray(Charsets.US_ASCII)
val iv = (stringIV.replace("-", "")).toByteArray(Charsets.US_ASCII)
aeskey.key = key
aeskey.iv = iv
val encryptedKey = cipher.doFinal(aesKey.Key)
The item that I am trying to encrypt is 64 bytes. Using this public key.
However, I get the following error:
Process: com.touchsides.rewards.debug, PID: 19470
com.android.org.bouncycastle.crypto.DataLengthException: input too large for RSA cipher.
at com.android.org.bouncycastle.crypto.engines.RSACoreEngine.convertInput(RSACoreEngine.java:115)
at com.android.org.bouncycastle.crypto.engines.RSABlindedEngine.processBlock(RSABlindedEngine.java:95)
at com.android.org.bouncycastle.crypto.encodings.OAEPEncoding.encodeBlock(OAEPEncoding.java:199)
at com.android.org.bouncycastle.crypto.encodings.OAEPEncoding.processBlock(OAEPEncoding.java:131)
I believe the modulus size is large enough to allow for the encryption of this byte array.
You get the com.android.org.bouncycastle.crypto.DataLengthException error because RSA key was created with negative Modulus. So it can't be used to encrypt even one byte.
BigInteger default constructor uses the most significant bit of provided byte array payload as sign bit for the number. So you have to explicitly specify that you want a positive number using another constructor where the first argument is a sign indicator:
BigInteger(1, posPublicKey.publicKey.modulus), BigInteger(1, posPublicKey.publicKey.exponent)
There's also a C# approach to add a zero-byte in front of the byte array (so the sign bit will be always zero):
BigInteger(byteArrayOf(0) + posPublicKey.publicKey.modulus), BigInteger(byteArrayOf(0) + posPublicKey.publicKey.exponent)
And finally, if you already have your BigInteger created the wrong way, it's possible to convert it:
var modulus = BigInteger(posPublicKey.publicKey.modulus)
if (modulus.compareTo(BigInteger.ZERO) < 0)
modulus = modulus.add(BigInteger.ONE.shiftLeft(4096))
Please also look at this answer and answers of this question.

not getting the original data after decompression with Inflater. inflate

Here's my test code, the problem is resultLength != original.length, and result array is same as original array, shouldn't it be after compression and decompression?
public static void main(String[] args) {
// generateTyreAlarmEvent()
byte[] original = {10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0};
byte[] buffer = new byte[original.length];
Deflater compresser = new Deflater();
compresser.setInput(original);
compresser.finish();
int compressedLen = compresser.deflate(buffer);
compresser.end();
Inflater decompressor = new Inflater();
decompressor.setInput(buffer, 0, compressedLen);
byte[] result = new byte[original.length];
int resultLength = 0;
try {
resultLength = decompressor.inflate(result);
} catch (DataFormatException e) {
}
decompressor.end();
// the problem is resultLength(63) != original.length(67), and result array is same as original array
}
The original byte array is (len is 67) :
{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0}
and the result is (len is 63) :
{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1}
Thanks!
You did not make the output buffer big enough. Just to test, I replaced byte[] buffer = new byte[original.length]; with byte[] buffer = new byte[1000];, and it worked fine.
If the data is not compressible, as this is, then the output will be larger than the input.

DataFormatException: incorrect header check in java.util.zip.InflaterInputStream

I use javafx WebView in my project and it began crash at one website.
Through debug i understood, that when page receives part of js-code server uses header "Content-Encoding:deflate", ignoring my request headers.
Main problem in inflate method of InflaterInputStream.
java.util.zip.ZipException: incorrect header check
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
I reproduced this error in simple method:
public byte[] makeTestRequest(String url) throws Exception {
InputStreamResponseListener listener = new InputStreamResponseListener();
Request request = httpClient
.newRequest(url);
request.send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
byte[] uncompressedData = new byte[65536];
if (response.getStatus() == 200) {
try(InputStream responseContent = listener.getInputStream()){
InputStream stream = new InflaterInputStream(responseContent);
int len, offset = 0;
while ((len = stream.read(uncompressedData , offset, uncompressedData.length-offset))>0) {
offset += len;
}
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return uncompressedData;
}
Error occurs in this method.
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/zip/Inflater.java#247
Inflator's buffer in this moment contains data:
[-52, 93, 105, 115, 19, -57, -70, -2, 43, 42, 127, -54, -87, -63, -55, 116, -49, -98, 111, 22, 96, 98, -57, -40, -114, 23, -74, -101, -5, -63, 24, -127, 29, -37, -40, 120, 97, 75, -91, -54, 24, 8, -71, 5, 9, 107, 78, 40, 3, 33, 108, 39, 124, 57, 39, 62, 24, -127, -15, 34, -2, -62, -24, 31, -35, 126, -33, 30, 75, -45, -45, 61, 51, 26, 105, 20, 83, -87, 20, -42, 72, -74, -34, -18, -89, -33, 125, -23, -17, 59, -26, -58, -58, -89, 74, -117, 71, 74, -13, 11, -109, -77, 103, 59, -66, 116, 40, -47, 109, 87, -73, -68, 125, 29, -89, -25, 103, 103, 106, -49, 117, -10, 122, 108, 124, 113, 118, 126, -95, -10, -56, -78, 108, -41, -13, 8, -75, -10, 117, 44, -52, -51, -50, 47, 46, 116, 124, -7, 63, -33, 119, 76, -98, -22, -8, -110, 24, -122, 109, -17, 99, 127, 122, -66, 116, 118, -79, 7, 30, -20, -21, -104, -102, 60, -53, 126, -24, 88, 40, -99, -103, 97, 15, 59, -40, -17, -80, 95, 25, -104, 63, 85, -102, 103, 79, 53, -83, 83, -45, 14, -112, -2, 18, 123, 126, 118, 108, -90, -60, 30, -7, 47, -85, 87, -85, 43, -2, 127, -3, -118, -65, -11, 121, -63, 95, -83, 94, -59, -97, 55, 11, -2, 29, -1, -91, -65, -38, -15, -61, -66, -32, -69, -22, 127, 26, -120, -112, -1, -80, -6, 79, -42, 126, -97, -102, 54, -11, -62, -76, -102, -87, -76, -114, 107, -102, 123, -48, -103, 10, -3, -31, -25, 126, -39, -33, 97, -1, 109, 84, -81, 48, 90, 31, 51, 90, -33, -79, 127, 95, 86, 111, 85, 127, -87, -34, -16, -53, -43, 101, 124, -2, -112, 125, -7, -102, -65, -59, 62, 117, -69, -10, -11, 102, 10, -7, -29, -22, 111, -87, 47, -33, 51, 28, 51, 59, -7, 93, 90, 118, -14, -1, -12, 55, -128, -8, -6, 87, 59, -60, -10, -78, -94, 60, 114, -48, -42, 15, -57, -94, -4, 79, 32, -124, -3, -5, -100, -67, 126, -29, -81, 85, -105, 25, 73, -1, -57, 8, -72, -30, 111, -78, 47, 47, 23, -4, 109, -10, 112, -123, -47, 20, -94, -62, -16, 44, 43, 76, -123, -95, -96, 98, -66, 116, -122, 29, 89, 120, -41, 54, -115, 40, 81, 68, -45, 102, -12, -119, -109, 33, -94, -18, -78, 111, -127, -81, 44, -121, 72, -5, -61, -81, 84, -81, -80, -89, 12, 60, -10, -22, 25, -37, -93, -113, -80, 51, -80, 35, -2, -70, -65, 86, -93, -57, 72, 1, -108, 36, 125, 79, 125, 85, -90, 103, -89, -82, 74, 90, -58, 56, -79, -10, -89, 44, -29, 87, -1, 13, 35, -70, 2, -80, 50, -46, -73, 16, -8, -73, 8, 55, -20, -7, -57, 78, 98, -121, 118, -106, -70, 70, -104, 6, -102, 74, -125, -50, -74, 82, 100, -71, 87, -64, -77, 64, -125, -1, 1, -72, -104, -67, 126, 84, 103]
I'm surprised, but my that page successfully displayed in Chrome and Safari, but don't work in javafx WebView.
Is it a bug in inflate method or i'm doing something wrong?
It didn't solve the problem with javafx WebView, but using Inflator constructor with nowrap option helped with incorrect header check" error:
Inflator inf = new Inflator(true);
InputStream stream = new InflaterInputStream(responseContent, inf);

Convert byte array (byte[]) to Image in Java

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.

Categories