1

I have the following function in Java(write inside an Android app)

Bitmap bm = BitmapFactory.decodeFile(stringPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the bitmap object  era quality 100
byte[] byteData = baos.toByteArray();
byte[] newB = Base64.encode(byteData,0);

It get an Image file and convert it to a byte array.

I need that to work on Swift.

I was able to convert the byteData to Int8, the equivalent of byte array in Swift.

I use the code below:

let filename = "RES4010110001"
let test_image:UIImage = UIImage(named: filename)!
let dataImage = UIImageJPEGRepresentation(test_image, 1)! as Data
var bytes: [Int8] = dataImage.map{Int8(bitPattern: $0)}

When I print this data using print(bytes) I got the same results on iOS and Android when I compare the "byteData" from Android and "bytes" from iOS

But I don't know what is the Java equivalent function below on Swift

Base64.encode(byteData,0);

How can I create an equivalent function on Swift of the function above?

For me, it appears that this function is encoding a byte array. But I have no idea of how I can encode a Int8 Array.

I said Int8 because for me, Int8 is the Swift equivalent of byte type in Java.

edit: I want to encode a Int8 array, I guess this is what the Java function (the function in the post title) is doing inside the Android app.

10
  • 2
    Don't try to encode the Int array -- encode the Data from UIImageJPEGRepresentation like I pointed you towards in the last question you asked about this. Commented Jul 1, 2021 at 23:44
  • This data that I want to convert is needed to create a JSON file to be send to a Java Server. What you suggests is that I convert using "dataImage.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)" and after that convert it to NSData and then finally to Int8 array, is that correct? Commented Jul 2, 2021 at 0:17
  • Edit: I need the data in this format [-1, -40, -1, -32, 0, 16, 74, 70, 73, ...], A String(base64 or not) is not an option, because the server only accepts data as a Java byte[]. I expect that I was able to explain my situation. Commented Jul 2, 2021 at 0:18
  • Your question asked for a base 64 encoding, so I assumed that's what you wanted. An array of Ints is not a base 64 encoding. Commented Jul 2, 2021 at 0:23
  • Sorry if my question title is confuse. I was talking about a Base64 encoding function on Java. The function on the title is running inside an Android app. I want to convert this function to run on iOS. This function is capable to encode a byte array. That's is what I was looking for. On Swift, the equivalent of Java byte array is an Int8 array. I want to encode a Int8 array. Commented Jul 2, 2021 at 0:33

2 Answers 2

1

You can use base64EncodedData to encode your Data to get an encoded Data, rather than working with [Int8].

The Java code passes 0 (DEFAULT) as the flags. According to the docs, this means it's compliant with RFC 2045. RFC 2045 says that lines should be no more than 76 characters, so we should pass lineLength76Characters. RFC 2045 also requires CRLF as line endings, but base64EncodedData seems to insert them automatically even if we don't pass endLineWithCarriageReturn and endLineWithLineFeed.

let filename = "RES4010110001"
if let testImage = UIImage(named: filename),
    let dataImage = testImage.jpegData(compressionQuality: 1) {
    let encodedData = dataImage.base64EncodedData(options: [.lineLength76Characters])
} else {
    // failed to get the UIImage, or the JPEG data, handle the error here...
}

I suggest that you do not work with [Int8] here. The byte[]s in the Java code are clearly not just "lists of numbers between -128 and 127". They represent data buffers, and those are represented by the Data struct in Swift. Don't worry, Both Data and [Int8] share a very similar set of protocol conformances, like MutableCollection, RangeReplaceableCollection, RandomAccessCollection etc. You can do almost everything you can do to an Int8 array, to a Data.

Sign up to request clarification or add additional context in comments.

Comments

0

I was able to find an definite answer using part of the answer created by @Sweeper and from another answer published here on Stackoverflow

let filename = "RES4010110001"
let test_image:UIImage = UIImage(named: filename)!
let dataImage = UIImageJPEGRepresentation(test_image, 1)! as Data // use apenas este que é correto, o galvez usa jpg no android
let dataImageStringBase64:String = dataImage.base64EncodedString(options: [.lineLength76Characters])
let byteArray = [UInt8](dataImageStringBase64.utf8)

The answer posted by @Sweeper just miss this line

let byteArray = [UInt8](dataImageStringBase64.utf8)

This last line of code made the code works perfectly for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.