Class Notes – Resize Camera Image – Out of Memory Issue

String imageFilePath = "myfavpict.jpg";
PImage cameraImage;

void setup() {
  orientation(LANDSCAPE);
  
  // Create a File object out of that
  File imageFile = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + imageFilePath);
  // Create a Uri out of that
  android.net.Uri imageFileUri = android.net.Uri.fromFile(imageFile);

  // Create the Intent that triggers the camera
  android.content.Intent i = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  // Tell the camera application where we want the resulting image saved
  i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
  // Start the Camera
  startActivityForResult(i, 0);
}

void draw() {
  if (cameraImage != null) {
    //image(cameraImage, 0, 0, width, height);
    cameraImage.loadPixels();
    loadPixels();
    
    for (int x = 0; x < cameraImage.width && x < width; x++) {
      for (int y = 0; y < cameraImage.height && y < height; y++) {
         int r = int(red(cameraImage.pixels[x+cameraImage.width*y]));
         color c = color(r,r,r);
         pixels[x+cameraImage.width*y] = c;
      }
    }
    updatePixels();
      filter(INVERT);
    
  }
}

protected void onActivityResult(int requestCode, int resultCode, android.content.Intent intent) {
  super.onActivityResult(requestCode, resultCode, intent);
  if (resultCode == RESULT_OK) {
    // We know the location via the imageFilePath String so load it into a standard Processing PImage
    println(imageFilePath);
    resizeImage(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + imageFilePath, android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/smallimage.jpg");
    cameraImage = loadImage(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "smallimage.jpg");
    println("Should have loaded Image");
  }
}


// Loads an image from disk at screen resolution and saves it back out at the same size.
void resizeImage(String imageFilePath, String newImageFilePath) {
  try {
    // Load up the image's dimensions not the image itself
    android.graphics.BitmapFactory.Options bmpFactoryOptions = new android.graphics.BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    android.graphics.Bitmap bmp = android.graphics.BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);
    println("HEIGHTRATIO: " + heightRatio);
    println("WIDTHRATIO: " + widthRatio);
    // If both of the ratios are greater than 1, one of the sides of // the image is greater than the screen
    if (heightRatio > 1 && widthRatio > 1) {
      if (heightRatio > widthRatio) {
        // Height ratio is larger, scale according to it
        bmpFactoryOptions.inSampleSize = heightRatio;
      } 
      else {
        // Width ratio is larger, scale according to it
        bmpFactoryOptions.inSampleSize = widthRatio;
      }
    }
    // Decode it for real
    bmpFactoryOptions.inJustDecodeBounds = false;
    bmp = android.graphics.BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

    java.io.File newImageFile = new java.io.File(newImageFilePath);
    android.net.Uri newImageUri = android.net.Uri.fromFile(newImageFile);
    OutputStream imageFileOS = getContentResolver().openOutputStream(newImageUri);
    bmp.compress(android.graphics.Bitmap.CompressFormat.JPEG, 90, imageFileOS);

    println("Resized image, wrote out to: " + newImageFile.getAbsolutePath());
  } 
  catch (Exception e) {
    println(e.toString());
    e.printStackTrace();
  }
}