0

I extended the Charm-Down Pictures Service and added a new method that would allow me to save an image in the user's camera roll. So I used information from previous questions to convert the image into a base64 encoded string.

@Override
public void saveImage(Image image) {
    PixelReader pixelReader = image.getPixelReader();
    int width = (int) image.getWidth();
    int height = (int) image.getHeight();
    int[] data = new int[width * height];
    pixelReader.getPixels(0, 0, width, height, PixelFormat.getIntArgbPreInstance(), data, 0, width);

    // 2. int array to byte array
    ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(data);


    // 3. Encode to String
    String encoded = Base64.getEncoder().encodeToString(arr);
    saveImage(encoded);
}

public static native void saveImage(String s);

The native method called Does this :

JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_plugins_ios_IOSPicturesService_saveImage
(JNIEnv *env, jclass jClass, jbyteArray array, jstring jTitle) {
    const jchar *charsTitle = (*env)->GetStringChars(env, jTitle, NULL);
    NSString *name = [NSString stringWithCharacters:(UniChar *)charsTitle length:(*env)->GetStringLength(env, jTitle)];
    (*env)->ReleaseStringChars(env, jTitle, charsTitle);

    NSData *data = [[NSData alloc]initWithBase64EncodedString:name options:NSDataBase64DecodingIgnoreUnknownCharacters];
    if (data==NULL) {
        NSLog(@"data is null");
    }
    NSLog(@"Saving");
    UIImage *ret = [UIImage imageWithData : data];
    if (ret==NULL) {
        NSLog(@"image is null");
    }
    [_pictures savePicture: ret];
}

-(void) savePicture : (UIImage *) snapshot {
    UIImageWriteToSavedPhotosAlbum(snapshot, nil,nil, nil);
    NSLog(@"Saved");
}

I used this code to convert a JavaFx Image into a string and natively turn it back into a UIImage that can be stored in the user's camera roll. However, everytime that I attempt this, the (data==NULL) block gets called because the NSData object appears to be null. Does anyone know why my Base64 encoded string cannot be converted into a NSData object properly?

    byte[] arr = byteBuffer.array();
    for (int x = 0; x < arr.length; x += 4) {
        byte b = arr[x];
        arr[x] = arr[x + 1];   //a  r
        arr[x + 1] = arr[x + 2];//r  g
        arr[x + 2] = arr[x + 3];//g  b
        arr[x + 3] = b;//b  a
    }

I even optionally converted it from argb to Rgba format, but this did not seem to fix the problem

Aniket Joshi
  • 115
  • 1
  • 8
  • Did you check that the encoded string you get in step 3 can be decoded in Java? Add some logging to the iOS part and see if `name` is not null and check its length. Maybe [this](https://stackoverflow.com/questions/21406482/nsdata-wont-accept-valid-base64-encoded-string) can help? – José Pereda Aug 09 '17 at 09:45
  • that fixed the "data is null" issue, but now the image, after It has been converted from a base64 string is showing as null – Aniket Joshi Aug 13 '17 at 18:51

0 Answers0