Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change image encoding over the wire to jpeg at 80% quality #32

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal fun com.google.ai.client.generativeai.type.Content.toInternal() =
internal fun com.google.ai.client.generativeai.type.Part.toInternal(): Part {
return when (this) {
is com.google.ai.client.generativeai.type.TextPart -> TextPart(text)
is ImagePart -> BlobPart(Blob("image/png", encodeBitmapToBase64Png(image)))
is ImagePart -> BlobPart(Blob("image/jpeg", encodeBitmapToBase64Png(image)))
is com.google.ai.client.generativeai.type.BlobPart ->
BlobPart(Blob(mimeType, Base64.encodeToString(blob, BASE_64_FLAGS)))
else ->
Expand Down Expand Up @@ -192,7 +192,7 @@ internal fun CountTokensResponse.toPublic() =

private fun encodeBitmapToBase64Png(input: Bitmap): String {
ByteArrayOutputStream().let {
input.compress(Bitmap.CompressFormat.PNG, 100, it)
input.compress(Bitmap.CompressFormat.JPEG, 80, it)
return Base64.encodeToString(it.toByteArray(), BASE_64_FLAGS)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ interface Part
/** Represents text or string based data sent to and received from requests. */
class TextPart(val text: String) : Part

/** Represents image data sent to and received from requests. */
/**
* Represents image data sent to and received from requests. When this is sent to the server it is
* converted to jpeg encoding at 80% quality.
*/
class ImagePart(val image: Bitmap) : Part

/** Represents binary data with an associated MIME type sent to and received from requests. */
Expand Down