Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.AttributeSet;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import java.io.File;
- import java.io.IOException;
- public class JsonGuiImageView extends LinearLayout {
- ImageView imageView;
- ImageButton button;
- Intent cameraIntent;
- Activity activity;
- Bitmap photo;
- Context context;
- int CAMERA_REQUEST = 1777;
- public JsonGuiImageView(Context c, Activity a){
- super(c);
- this.context = c;
- this.activity = a;
- button = new ImageButton(c);
- button.setLayoutParams(new ViewGroup.LayoutParams(60, 60));
- button.setImageResource(R.drawable.ic_add);
- button.setMaxHeight(60);
- button.setMinimumHeight(60);
- button.setMaxWidth(60);
- button.setMinimumWidth(60);
- button.setOnClickListener(AddImage);
- this.addView(button);
- }
- public JsonGuiImageView(Context context, AttributeSet attributeSet){
- super(context, attributeSet);
- }
- OnClickListener AddImage = new OnClickListener() {
- @Override
- public void onClick(View view) {
- File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
- cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
- if (cameraIntent.resolveActivity(activity.getPackageManager()) != null) {
- activity.startActivityForResult(cameraIntent);
- }
- }
- };
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data){
- if(requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
- File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
- photo = decodeSampledBitmapFromFile(file.getAbsolutePath(), 480, 640);
- imageView = new ImageView(getContext());
- imageView.setMaxHeight(60);
- imageView.setMinimumHeight(60);
- imageView.setMaxWidth(60);
- imageView.setMinimumWidth(60);
- imageView.setImageBitmap(photo);
- }
- }
- public static Bitmap decodeSampledBitmapFromFile(String path, 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(path, 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 SampSize..
- 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(path);
- 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(path, 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement