Advertisement
radityakurnianto

ProcessImage

Oct 16th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.86 KB | None | 0 0
  1. package kolek.com.kolekin;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Matrix;
  9. import android.media.ExifInterface;
  10. import android.net.Uri;
  11. import android.os.Environment;
  12. import android.provider.MediaStore;
  13. import android.util.Base64;
  14. import android.util.Log;
  15. import android.widget.ImageView;
  16.  
  17. import org.json.JSONArray;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28.  
  29. /**
  30.  * Created by Raditya on 10/13/2014.
  31.  * This class handle bitmap image operation and
  32.  * hold all dynamic image and image path.
  33.  * Image path will be store to local db
  34.  */
  35. public class ProcessImage {
  36.     Intent cameraIntent;
  37.     String PATH;
  38.     ArrayList<Bitmap> listBitmap = new ArrayList<Bitmap>();
  39.  
  40.     public void TakeImage(Context context, Activity activity, int request){
  41.         File photoFile = null;
  42.         cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  43.         // Check if there is available intent to handle camera
  44.         if(cameraIntent.resolveActivity(context.getPackageManager()) != null){
  45.             try{
  46.                 // Create a photo file
  47.                 photoFile = CreateImageFile();
  48.             } catch (IOException e){ }
  49.  
  50.             if (photoFile != null){
  51.                 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  52.                 activity.startActivityForResult(cameraIntent, request);
  53.             }
  54.         }
  55.     }
  56.  
  57.     public File CreateImageFile() throws IOException{
  58.         String imgTimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  59.         String imgFileName = "img_" + imgTimeStamp + ".png";
  60.         String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/KolekinData/";
  61.  
  62.         // Create directory if directory not exist
  63.         File dir = new File(storageDir);
  64.         if(!dir.exists()){
  65.             dir.mkdir();
  66.         }
  67.  
  68.         // Save current image to storageDir folder
  69.         File imageFile = new File(storageDir + imgFileName);
  70.         Log.d("IMAGE PATH", imageFile.getAbsolutePath());
  71.         setPath(imageFile.getAbsolutePath());
  72.         return imageFile;
  73.     }
  74.  
  75.     public void setPath(String p){
  76.         this.PATH = p;
  77.         Log.d("Set Path", this.PATH);
  78.     }
  79.  
  80.     public String getPath(){
  81.         Log.d("Get Path", PATH);
  82.         return PATH;
  83.     }
  84.  
  85. //    public Bitmap setPic(String path) {
  86. //        // Get the dimensions of the View
  87. //        int targetW = 480;
  88. //        int targetH = 480;
  89. //
  90. //        // Get the dimensions of the bitmap
  91. //        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
  92. //        bmOptions.inJustDecodeBounds = true;
  93. //        BitmapFactory.decodeFile(path, bmOptions);
  94. //        int photoW = bmOptions.outWidth;
  95. //        int photoH = bmOptions.outHeight;
  96. //
  97. //        // Determine how much to scale down the image
  98. //        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
  99. //
  100. //        // Decode the image file into a Bitmap sized to fill the View
  101. //        bmOptions.inJustDecodeBounds = false;
  102. //        bmOptions.inSampleSize = scaleFactor << 1;
  103. //        bmOptions.inPurgeable = true;
  104. //
  105. //        Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
  106. //
  107. //        Matrix mtx = new Matrix();
  108. //        mtx.postRotate(90);
  109. //        // Rotating Bitmap
  110. //        Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);
  111. //
  112. //        if (rotatedBMP != bitmap)
  113. //            bitmap.recycle();
  114. //
  115. //        // return the bitmap here
  116. //        return rotatedBMP;
  117. //    }
  118.  
  119.     public Bitmap decodeSampledBitmapFromFile(int reqWidth, int reqHeight) {
  120.         Bitmap decode, rotatedBitmap = null;
  121.  
  122.         //First decode with inJustDecodeBounds=true to check dimensions
  123.         final BitmapFactory.Options options = new BitmapFactory.Options();
  124.         options.inJustDecodeBounds = true;
  125.         BitmapFactory.decodeFile(getPath(), options);
  126.  
  127.         // Calculate inSampleSize, Raw height and width of image
  128.         final int height = options.outHeight;
  129.         final int width = options.outWidth;
  130.         options.inPreferredConfig = Bitmap.Config.RGB_565;
  131.         int inSampleSize = 8;
  132.  
  133.         if (height > reqHeight)
  134.         {
  135.             inSampleSize = Math.round((float)height / (float)reqHeight);
  136.         }
  137.         int expectedWidth = width / inSampleSize;
  138.  
  139.         if (expectedWidth > reqWidth)
  140.         {
  141.             //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampleSize..
  142.             inSampleSize = Math.round((float)width / (float)reqWidth);
  143.         }
  144.  
  145.         options.inSampleSize = inSampleSize;
  146.  
  147.         // Decode bitmap with inSampleSize set
  148.         options.inJustDecodeBounds = false;
  149.         options.inPurgeable = true;
  150.         options.inInputShareable = true;
  151.         options.inTempStorage = new byte[16 * 1024];
  152.  
  153.         try{
  154.             ExifInterface exifInterface = new ExifInterface(getPath());
  155.             int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  156.  
  157.             int rotation = 0;
  158.             switch (orientation){
  159.                 case ExifInterface.ORIENTATION_ROTATE_90 :
  160.                     rotation = 90;
  161.                     break;
  162.                 case ExifInterface.ORIENTATION_ROTATE_180 :
  163.                     rotation = 180;
  164.                     break;
  165.                 case ExifInterface.ORIENTATION_ROTATE_270 :
  166.                     rotation = 270;
  167.                     break;
  168.                 default: break;
  169.             }
  170.  
  171.             Matrix matrix = new Matrix();
  172.             matrix.postRotate(rotation);
  173.  
  174.             decode = BitmapFactory.decodeFile(getPath(), options);
  175.             rotatedBitmap = Bitmap.createBitmap(decode, 0, 0, decode.getWidth(), decode.getHeight(), matrix, true);
  176.         } catch (IOException e){
  177.             Log.d("ROTATION ERROR", e.getCause() + " " + e.getMessage());
  178.         }
  179.         return rotatedBitmap;
  180.     }
  181.  
  182. //    // Method to hold image
  183.     public void setListBitmap(Bitmap bitmap){
  184.         if(listBitmap.size() <= 2) {
  185.             listBitmap.add(bitmap);
  186.             Log.d("Image List", String.valueOf(listBitmap.size()));
  187.         }
  188.     }
  189. //
  190. //    // Remove bitmap
  191. //    public void removeBitmap(int position){
  192. //        if(listBitmap.size() <= 2){
  193. //            listBitmap.remove(position);
  194. //            Log.d("Image List", String.valueOf(listBitmap.size()));
  195. //        }
  196. //    }
  197. //
  198. //    // Convert image to base64 and encode as json
  199. //    public String Base64ToJson(){
  200. //        JSONObject jsonOuterObject = new JSONObject();
  201. //        JSONObject jsonInnerObject = new JSONObject();
  202. //        JSONArray jsonArray = new JSONArray();
  203. //        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  204. //        try {
  205. //            for (int i = 0; i < listBitmap.size(); i++) {
  206. //                listBitmap.get(i).compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
  207. //                byte[] byteArray = byteArrayOutputStream.toByteArray();
  208. //                jsonInnerObject.put("image" + i, Base64.encodeToString(byteArray, Base64.DEFAULT));
  209. //            }
  210. //            jsonArray.put(jsonInnerObject);
  211. //            jsonOuterObject.put("form_values_image", jsonArray);
  212. //            return jsonOuterObject.toString();
  213. //        }catch (JSONException e){
  214. //            Log.d("Process Image", "Error converting to json caused by" + e.getCause() + " Message : " + e.getMessage());
  215. //        }
  216. //        return jsonOuterObject.toString();
  217. //    }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement