Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kolek.com.kolekin;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.media.ExifInterface;
- import android.net.Uri;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.util.Base64;
- import android.util.Log;
- import android.widget.ImageView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- /**
- * Created by Raditya on 10/13/2014.
- * This class handle bitmap image operation and
- * hold all dynamic image and image path.
- * Image path will be store to local db
- */
- public class ProcessImage {
- Intent cameraIntent;
- String PATH;
- ArrayList<Bitmap> listBitmap = new ArrayList<Bitmap>();
- public void TakeImage(Context context, Activity activity, int request){
- File photoFile = null;
- cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- // Check if there is available intent to handle camera
- if(cameraIntent.resolveActivity(context.getPackageManager()) != null){
- try{
- // Create a photo file
- photoFile = CreateImageFile();
- } catch (IOException e){ }
- if (photoFile != null){
- cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
- activity.startActivityForResult(cameraIntent, request);
- }
- }
- }
- public File CreateImageFile() throws IOException{
- String imgTimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- String imgFileName = "img_" + imgTimeStamp + ".png";
- String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/KolekinData/";
- // Create directory if directory not exist
- File dir = new File(storageDir);
- if(!dir.exists()){
- dir.mkdir();
- }
- // Save current image to storageDir folder
- File imageFile = new File(storageDir + imgFileName);
- Log.d("IMAGE PATH", imageFile.getAbsolutePath());
- setPath(imageFile.getAbsolutePath());
- return imageFile;
- }
- public void setPath(String p){
- this.PATH = p;
- Log.d("Set Path", this.PATH);
- }
- public String getPath(){
- Log.d("Get Path", PATH);
- return PATH;
- }
- // public Bitmap setPic(String path) {
- // // Get the dimensions of the View
- // int targetW = 480;
- // int targetH = 480;
- //
- // // Get the dimensions of the bitmap
- // BitmapFactory.Options bmOptions = new BitmapFactory.Options();
- // bmOptions.inJustDecodeBounds = true;
- // BitmapFactory.decodeFile(path, bmOptions);
- // int photoW = bmOptions.outWidth;
- // int photoH = bmOptions.outHeight;
- //
- // // Determine how much to scale down the image
- // int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
- //
- // // Decode the image file into a Bitmap sized to fill the View
- // bmOptions.inJustDecodeBounds = false;
- // bmOptions.inSampleSize = scaleFactor << 1;
- // bmOptions.inPurgeable = true;
- //
- // Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
- //
- // Matrix mtx = new Matrix();
- // mtx.postRotate(90);
- // // Rotating Bitmap
- // Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);
- //
- // if (rotatedBMP != bitmap)
- // bitmap.recycle();
- //
- // // return the bitmap here
- // return rotatedBMP;
- // }
- public Bitmap decodeSampledBitmapFromFile(int reqWidth, int reqHeight) {
- Bitmap decode, rotatedBitmap = null;
- //First decode with inJustDecodeBounds=true to check dimensions
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(getPath(), options);
- // Calculate inSampleSize, Raw height and width of image
- final int height = options.outHeight;
- final int width = options.outWidth;
- options.inPreferredConfig = Bitmap.Config.RGB_565;
- int inSampleSize = 8;
- if (height > reqHeight)
- {
- inSampleSize = Math.round((float)height / (float)reqHeight);
- }
- int expectedWidth = width / inSampleSize;
- if (expectedWidth > reqWidth)
- {
- //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampleSize..
- inSampleSize = Math.round((float)width / (float)reqWidth);
- }
- options.inSampleSize = inSampleSize;
- // Decode bitmap with inSampleSize set
- options.inJustDecodeBounds = false;
- options.inPurgeable = true;
- options.inInputShareable = true;
- options.inTempStorage = new byte[16 * 1024];
- try{
- ExifInterface exifInterface = new ExifInterface(getPath());
- int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
- int rotation = 0;
- switch (orientation){
- case ExifInterface.ORIENTATION_ROTATE_90 :
- rotation = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180 :
- rotation = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270 :
- rotation = 270;
- break;
- default: break;
- }
- Matrix matrix = new Matrix();
- matrix.postRotate(rotation);
- decode = BitmapFactory.decodeFile(getPath(), options);
- rotatedBitmap = Bitmap.createBitmap(decode, 0, 0, decode.getWidth(), decode.getHeight(), matrix, true);
- } catch (IOException e){
- Log.d("ROTATION ERROR", e.getCause() + " " + e.getMessage());
- }
- return rotatedBitmap;
- }
- // // Method to hold image
- public void setListBitmap(Bitmap bitmap){
- if(listBitmap.size() <= 2) {
- listBitmap.add(bitmap);
- Log.d("Image List", String.valueOf(listBitmap.size()));
- }
- }
- //
- // // Remove bitmap
- // public void removeBitmap(int position){
- // if(listBitmap.size() <= 2){
- // listBitmap.remove(position);
- // Log.d("Image List", String.valueOf(listBitmap.size()));
- // }
- // }
- //
- // // Convert image to base64 and encode as json
- // public String Base64ToJson(){
- // JSONObject jsonOuterObject = new JSONObject();
- // JSONObject jsonInnerObject = new JSONObject();
- // JSONArray jsonArray = new JSONArray();
- // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- // try {
- // for (int i = 0; i < listBitmap.size(); i++) {
- // listBitmap.get(i).compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
- // byte[] byteArray = byteArrayOutputStream.toByteArray();
- // jsonInnerObject.put("image" + i, Base64.encodeToString(byteArray, Base64.DEFAULT));
- // }
- // jsonArray.put(jsonInnerObject);
- // jsonOuterObject.put("form_values_image", jsonArray);
- // return jsonOuterObject.toString();
- // }catch (JSONException e){
- // Log.d("Process Image", "Error converting to json caused by" + e.getCause() + " Message : " + e.getMessage());
- // }
- // return jsonOuterObject.toString();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement