Resize Images from Camera

Asked

Viewed 1,653 times

2

I’m working on an application that accesses the photo library and updates an Imageview with the selected photo in the mobile library. I have an LG G2 and my camera is configured by default to take pictures with the resolution 4160x2340. If I select a photo at this resolution, it will not fit on the app screen. The app does not return any error because it manages to treat the image, but it is not displayed due to the size. If I change the settings to 1280x960, for example, the condition works perfectly and the photo is successfully loaded.

I would like to resize the photo by leaving it to a default size regardless of the setting or resolution the photo was taken.

As a beginner in development, if someone can help me by telling me where to fix or add directly to my code, it would help me a lot.

My code:

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends ActionBarActivity {

ImageView img;
private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.imageView);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            abrirFoto();
        }

    });
}

public void abrirFoto() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    InputStream stream = null;
    if (requestCode == 1 && resultCode == RESULT_OK) {
        try {
            if (bitmap != null) {
                bitmap.recycle();
            }
            stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            img.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }
}

private static Bitmap resizeImage(Context context, Bitmap bmpOriginal,
                                  float newWidth, float newHeight) {
    Bitmap novoBmp = null;

    int w = bmpOriginal.getWidth();
    int h = bmpOriginal.getHeight();

    float densityFactor = context.getResources().getDisplayMetrics().density;
    float novoW = newWidth * densityFactor;
    float novoH = newHeight * densityFactor;

    //Calcula escala em percentagem do tamanho original para o novo tamanho
    float scalaW = novoW / w;
    float scalaH = novoH / h;

    // Criando uma matrix para manipulação da imagem BitMap
    Matrix matrix = new Matrix();

    // Definindo a proporção da escala para o matrix
    matrix.postScale(scalaW, scalaH);

    //criando o novo BitMap com o novo tamanho
    novoBmp = Bitmap.createBitmap(bmpOriginal, 0, 0, w, h, matrix, true);

    return novoBmp;

}
}

1 answer

2


I don’t understand why the larger images aren’t shown.

I use this function to resize images:

private static Bitmap resizeImage(Context context, Bitmap bmpOriginal,
                                  float newWidth, float newWeight) {
    Bitmap novoBmp = null;

    int w = bmpOriginal.getWidth();
    int h = bmpOriginal.getHeight();

    float densityFactor = context.getResources().getDisplayMetrics().density;
    float novoW = newWidth * densityFactor;
    float novoH = newHeight * densityFactor;

    //Calcula escala em percentagem do tamanho original para o novo tamanho
    float scalaW = novoW / w;
    float scalaH = novoH / h;

    // Criando uma matrix para manipulação da imagem BitMap
    Matrix matrix = new Matrix();

    // Definindo a proporção da escala para o matrix
    matrix.postScale(scalaW, scalaH);

    //criando o novo BitMap com o novo tamanho
    novoBmp = Bitmap.createBitmap(bmpOriginal, 0, 0, w, h, matrix, true);

    return novoBmp;
}

You can use it like this:

img.setImageBitmap(resizeImage(this, bitmap, newWidth, newHeight));
  • How do I call the "img.setImageBitmap(resizeImage(context, image, newWidth, newHeight));" command inside my onActivityResult? I edited my code the way you passed it, I’m just confused on how to call it. The updated code is here in my own question. @ramaral

  • Replace the line img.setImageBitmap(bitmap); for img.setImageBitmap(resizeImage(this, bitmap, newWidth, newHeight)); newWidth and newHeight replace by the values you want for the width and height of the image

  • Perfect!!!! @ramaral

  • Just one more thing, how would I integrate that code you passed to invert the photo "http://answall.com/questions/46684/inverter-foto-depois-de-actualizr-no-imageview-do-aplicativo-android" into that code? I played it in this code and generated some errors, in "filename" in the code "Exifinterface Exif = new Exifinterface(filename);" and in the catch exception (Ioexception e) { e.printStackTrace(); Return null; } @ramaral

  • Here it is necessary one more step because ExifInterface(filename) receives a string with the path to the image. What you have is a Uri returned by data.getData(). The solution is to convert this Uri into a string. A function to do this can be found here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.