1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 package com.example.executorchllamademo; 10 11 import android.content.ContentResolver; 12 import android.graphics.Bitmap; 13 import android.graphics.BitmapFactory; 14 import android.graphics.Color; 15 import android.net.Uri; 16 import androidx.annotation.Nullable; 17 import java.io.FileNotFoundException; 18 import java.io.InputStream; 19 20 public class ETImage { 21 private int width; 22 private int height; 23 private final byte[] bytes; 24 private final Uri uri; 25 private final ContentResolver contentResolver; 26 ETImage(ContentResolver contentResolver, Uri uri)27 ETImage(ContentResolver contentResolver, Uri uri) { 28 this.contentResolver = contentResolver; 29 this.uri = uri; 30 bytes = getBytesFromImageURI(uri); 31 } 32 getWidth()33 public int getWidth() { 34 return width; 35 } 36 getHeight()37 public int getHeight() { 38 return height; 39 } 40 getUri()41 public Uri getUri() { 42 return uri; 43 } 44 getBytes()45 public byte[] getBytes() { 46 return bytes; 47 } 48 getInts()49 public int[] getInts() { 50 // We need to convert the byte array to an int array because 51 // the runner expects an int array as input. 52 int[] intArray = new int[bytes.length]; 53 for (int i = 0; i < bytes.length; i++) { 54 intArray[i] = (bytes[i++] & 0xFF); 55 } 56 return intArray; 57 } 58 getBytesFromImageURI(Uri uri)59 private byte[] getBytesFromImageURI(Uri uri) { 60 try { 61 int RESIZED_IMAGE_WIDTH = 336; 62 Bitmap bitmap = resizeImage(uri, RESIZED_IMAGE_WIDTH); 63 64 if (bitmap == null) { 65 ETLogging.getInstance().log("Unable to get bytes from Image URI. Bitmap is null"); 66 return new byte[0]; 67 } 68 69 width = bitmap.getWidth(); 70 height = bitmap.getHeight(); 71 72 byte[] rgbValues = new byte[width * height * 3]; 73 74 for (int y = 0; y < height; y++) { 75 for (int x = 0; x < width; x++) { 76 // Get the color of the current pixel 77 int color = bitmap.getPixel(x, y); 78 79 // Extract the RGB values from the color 80 int red = Color.red(color); 81 int green = Color.green(color); 82 int blue = Color.blue(color); 83 84 // Store the RGB values in the byte array 85 rgbValues[y * width + x] = (byte) red; 86 rgbValues[(y * width + x) + height * width] = (byte) green; 87 rgbValues[(y * width + x) + 2 * height * width] = (byte) blue; 88 } 89 } 90 return rgbValues; 91 } catch (FileNotFoundException e) { 92 throw new RuntimeException(e); 93 } 94 } 95 96 @Nullable resizeImage(Uri uri, int maxLength)97 private Bitmap resizeImage(Uri uri, int maxLength) throws FileNotFoundException { 98 InputStream inputStream = contentResolver.openInputStream(uri); 99 if (inputStream == null) { 100 ETLogging.getInstance().log("Unable to resize image, input streams is null"); 101 return null; 102 } 103 Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 104 if (bitmap == null) { 105 ETLogging.getInstance().log("Unable to resize image, bitmap during decode stream is null"); 106 return null; 107 } 108 109 float aspectRatio; 110 int finalWidth, finalHeight; 111 112 if (bitmap.getWidth() > bitmap.getHeight()) { 113 // width > height --> width = maxLength, height scale with aspect ratio 114 aspectRatio = bitmap.getWidth() / (float) bitmap.getHeight(); 115 finalWidth = maxLength; 116 finalHeight = Math.round(maxLength / aspectRatio); 117 } else { 118 // height >= width --> height = maxLength, width scale with aspect ratio 119 aspectRatio = bitmap.getHeight() / (float) bitmap.getWidth(); 120 finalHeight = maxLength; 121 finalWidth = Math.round(maxLength / aspectRatio); 122 } 123 124 return Bitmap.createScaledBitmap(bitmap, finalWidth, finalHeight, false); 125 } 126 } 127