import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by ricksam on 19/11/2016.
* Manifest:
* <uses-permission android:name="android.permission.CAMERA" />
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
* <uses-feature
* android:name="android.hardware.camera"
* android:required="false" />
* <uses-feature
* android:name="android.hardware.camera.front"
* android:required="false" />
*/
public class PhotoHelper {
//Photo
static final int REQUEST_TAKE_PHOTO = 1;
static String mCurrentPhotoPath = "";
private Activity activity;
public PhotoHelper(Activity activity){
this.activity = activity;
}
public void takePhotoIntent(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
//...
//Toast.makeText(view, ex.getMessage(), Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
//Toast.makeText(view, "NFC available", Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
//takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
activity.startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
/*
* Use in Activity
* @Override
* protected void onActivityResult(int requestCode, int resultCode, Intent data) {
* super.onActivityResult(requestCode, resultCode, data);
* image_photo.setImageBitmap(photoHelper.getPhoto(requestCode, resultCode));
* }
* */
public Bitmap getPhoto(int requestCode, int resultCode){
Bitmap bmp = null;
try {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == activity.RESULT_OK) {
File file = new File(mCurrentPhotoPath);
//sendImagem(file);
for (int i = 0; i < 10; i++){ bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
if(bmp!=null){
break; }else{ SystemClock.sleep(500); } }
}
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
public Bitmap scalePhoto(Bitmap source, int dstWidth, int dstHeight){ if(source.getWidth()>source.getHeight()){ source = Bitmap.createScaledBitmap(source, dstWidth, dstHeight, false); } else{ source = Bitmap.createScaledBitmap(source, dstHeight, dstWidth, false); } return source; } public String getFotoBase64(Bitmap source){ source = scalePhoto(source, 640, 360); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); source.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream .toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); }
public File getPhotoFile(){ return new File(mCurrentPhotoPath); }
public Bitmap squarePhoto(Bitmap source){ if (source.getWidth() >= source.getHeight()){ return Bitmap.createBitmap( source, source.getWidth()/2 - source.getHeight()/2, 0, source.getHeight(), source.getHeight() ); }else{ return Bitmap.createBitmap( source, 0, source.getHeight()/2 - source.getWidth()/2, source.getWidth(), source.getWidth() ); }
}
}