Skip to content

Instantly share code, notes, and snippets.

@PepeuCps
Created October 15, 2013 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PepeuCps/6997421 to your computer and use it in GitHub Desktop.
Save PepeuCps/6997421 to your computer and use it in GitHub Desktop.
Exemplo de como tirar uma fotografia e redimensionar a imagem. Exemplo usando Thread e ProgressDialog.
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
public class TakePicture extends Activity implements Runnable {
//Variaveis
private static final int PICTURE_RESULT = 1001;
private String fileName = "seucaminho\foto_%s.jpg";
private File fileResult;
private Uri outputFileUri;
private File outputFile;
ProgressDialog progress;
boolean isProcessed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tirar_foto);
//Caminho da foto
fileName = String.format(fileName, System.currentTimeMillis());
//Arquivo da foto
fileResult = new File(fileName);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.nova_foto:
outputFileUri = Uri.fromFile(fileResult);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, PICTURE_RESULT);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == PICTURE_RESULT) {
progress = ProgressDialog.show(this, "", "Processando...", true, false);
Thread thread = new Thread(this);
thread.start();
}
}
public static boolean processPicture(File fileFrom) {
FileOutputStream out;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bmpCompressed = resizeImage(fileFrom);
if (bmpCompressed != null){
out = new FileOutputStream(fileFrom.getPath());
bmpCompressed.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.flush();
out.close();
}
System.gc();
return true;
} catch (Exception e) {
Log.e("TakePicture", "processPicture", e);
return false;
}
}
public static Bitmap resizeImage(File file){
try {
final int IMAGE_MAX_SIZE = 500000; // 50kb
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getPath(), o);
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Bitmap b = null;
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeFile(file.getPath(), o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
double y = Math.sqrt(IMAGE_MAX_SIZE / (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeFile(file.getPath());
}
if(b!=null)
Log.d("TakePicture", "bitmap size - width: " +b.getWidth() + ", height: " + b.getHeight());
else
Log.d("TakePicture", "bitmap size - b null");
return b;
} catch (Exception e) {
Log.e("TakePicture", e.getMessage(), e);
return null;
}
}
@Override
public void run() {
isProcessed = processPicture(outputFile);
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(isProcessed) {
//AQUI FAÇA O TRATAMENTO SE SUA IMAGEM FOI CRIADA COM SUCESSO
} else {
Toast.makeText(getApplicationContext(), "Erro ao processar a imagem", Toast.LENGTH_SHORT).show();
}
progress.dismiss();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment