Skip to content

Commit

Permalink
Change image encoding over the wire to jpeg at 80% quality (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: David Motsonashvili <davidmotson@google.com>
  • Loading branch information
davidmotson and David Motsonashvili authored Jan 3, 2024
1 parent a125ac6 commit 9f52c2d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
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

0 comments on commit 9f52c2d

Please sign in to comment.