Advertisement
radityakurnianto

Dynamic button

Oct 13th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Matrix;
  7. import android.media.ExifInterface;
  8. import android.net.Uri;
  9. import android.os.Environment;
  10. import android.provider.MediaStore;
  11. import android.util.AttributeSet;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.ImageButton;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;
  18.  
  19. import java.io.File;
  20. import java.io.IOException;
  21.  
  22.  
  23. public class JsonGuiImageView extends LinearLayout {
  24.     ImageView imageView;
  25.     ImageButton button;
  26.     Intent cameraIntent;
  27.     Activity activity;
  28.     Bitmap photo;
  29.     Context context;
  30.     int CAMERA_REQUEST = 1777;
  31.  
  32.     public JsonGuiImageView(Context c, Activity a){
  33.         super(c);
  34.         this.context = c;
  35.         this.activity = a;
  36.  
  37.         button = new ImageButton(c);
  38.         button.setLayoutParams(new ViewGroup.LayoutParams(60, 60));
  39.         button.setImageResource(R.drawable.ic_add);
  40.         button.setMaxHeight(60);
  41.         button.setMinimumHeight(60);
  42.         button.setMaxWidth(60);
  43.         button.setMinimumWidth(60);
  44.         button.setOnClickListener(AddImage);
  45.         this.addView(button);
  46.     }
  47.  
  48.     public JsonGuiImageView(Context context, AttributeSet attributeSet){
  49.         super(context, attributeSet);
  50.     }
  51.  
  52.     OnClickListener AddImage = new OnClickListener() {
  53.         @Override
  54.         public void onClick(View view) {
  55.             File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
  56.             cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  57.             cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
  58.             if (cameraIntent.resolveActivity(activity.getPackageManager()) != null) {
  59.                 activity.startActivityForResult(cameraIntent);
  60.             }
  61.         }
  62.     };
  63.  
  64.     @Override
  65.     protected void  onActivityResult(int requestCode, int resultCode, Intent data){
  66.         if(requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
  67.             File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
  68.             photo = decodeSampledBitmapFromFile(file.getAbsolutePath(), 480, 640);
  69.             imageView = new ImageView(getContext());
  70.             imageView.setMaxHeight(60);
  71.             imageView.setMinimumHeight(60);
  72.             imageView.setMaxWidth(60);
  73.             imageView.setMinimumWidth(60);
  74.             imageView.setImageBitmap(photo);
  75.         }
  76.     }
  77.  
  78.     public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
  79.     {
  80.         Bitmap decode, rotatedBitmap = null;
  81.  
  82.         //First decode with inJustDecodeBounds=true to check dimensions
  83.         final BitmapFactory.Options options = new BitmapFactory.Options();
  84.         options.inJustDecodeBounds = true;
  85.         BitmapFactory.decodeFile(path, options);
  86.  
  87.         // Calculate inSampleSize, Raw height and width of image
  88.         final int height = options.outHeight;
  89.         final int width = options.outWidth;
  90.         options.inPreferredConfig = Bitmap.Config.RGB_565;
  91.         int inSampleSize = 8;
  92.  
  93.         if (height > reqHeight)
  94.         {
  95.             inSampleSize = Math.round((float)height / (float)reqHeight);
  96.         }
  97.         int expectedWidth = width / inSampleSize;
  98.  
  99.         if (expectedWidth > reqWidth)
  100.         {
  101.             //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
  102.             inSampleSize = Math.round((float)width / (float)reqWidth);
  103.         }
  104.  
  105.         options.inSampleSize = inSampleSize;
  106.  
  107.         // Decode bitmap with inSampleSize set
  108.         options.inJustDecodeBounds = false;
  109.         options.inPurgeable = true;
  110.         options.inInputShareable = true;
  111.         options.inTempStorage = new byte[16 * 1024];
  112.  
  113.         try{
  114.             ExifInterface exifInterface = new ExifInterface(path);
  115.             int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  116.  
  117.             int rotation = 0;
  118.             switch (orientation){
  119.                 case ExifInterface.ORIENTATION_ROTATE_90 :
  120.                     rotation = 90;
  121.                     break;
  122.                 case ExifInterface.ORIENTATION_ROTATE_180 :
  123.                     rotation = 180;
  124.                     break;
  125.                 case ExifInterface.ORIENTATION_ROTATE_270 :
  126.                     rotation = 270;
  127.                     break;
  128.                 default: break;
  129.             }
  130.  
  131.             Matrix matrix = new Matrix();
  132.             matrix.postRotate(rotation);
  133.  
  134.             decode = BitmapFactory.decodeFile(path, options);
  135.             rotatedBitmap = Bitmap.createBitmap(decode, 0, 0, decode.getWidth(), decode.getHeight(), matrix, true);
  136.         } catch (IOException e){
  137.             Log.d("ROTATION ERROR", e.getCause() + " " + e.getMessage());
  138.         }
  139.  
  140.         return rotatedBitmap;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement