Resizing Camera Images

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 set by default to take photos with the resolution 4160x2340. If I select a photo at that resolution, it does not fit on the application screen. The app does not return any error because it ends up handling the image, but it is not displayed because of the size. If I change the settings to 1280x960, for example, the condition works perfectly and the photo loads successfully.

I would like to redimension the photo leaving a default size regardless of the setting or resolution the picture was taken.

As a beginner in development, if someone can help me by telling me where to correct 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;

}
}
    
asked by anonymous 15.01.2015 / 19:05

1 answer

2

I do not understand why the larger images are not displayed.

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));
    
15.01.2015 / 20:02