From 2db1254750747376fb87775b85e40cd1b5a05910 Mon Sep 17 00:00:00 2001 From: Felix Atsma Date: Wed, 28 Jun 2017 10:25:54 +0200 Subject: [PATCH] Image preview working --- .../tagram/CameraFragment.java | 24 +------- .../myhyvesbookplus/tagram/CameraPreview.java | 8 +-- .../tagram/PicturePreview.java | 59 ++----------------- 3 files changed, 9 insertions(+), 82 deletions(-) diff --git a/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraFragment.java b/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraFragment.java index c45adc3..900666d 100644 --- a/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraFragment.java +++ b/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraFragment.java @@ -4,7 +4,6 @@ import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Matrix; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.net.Uri; @@ -46,7 +45,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL private Camera mCamera; private CameraPreview mPreview; - private byte[] mPhotoRaw; private Bitmap mPhoto; private int facing = Camera.CameraInfo.CAMERA_FACING_BACK; @@ -99,7 +97,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL final RelativeLayout pictureButtons = (RelativeLayout) view.findViewById(R.id.picture_taken_buttons); final RelativeLayout filterButtons = (RelativeLayout) view.findViewById(R.id.filter_buttons); final RelativeLayout mCameraLayout = (RelativeLayout) view.findViewById(R.id.camera_preview); -// final RelativeLayout mImageTaken = (RelativeLayout) view.findViewById(R.id.picture_view); mCameraLayout.addView(mPreview); @@ -131,13 +128,9 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL mCamera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { -// Bitmap bmp = rotate(BitmapFactory.decodeByteArray(data, 0, data.length, null), 90); -// mPhoto = bmp; mPhoto = BitmapFactory.decodeByteArray(data, 0, data.length, null); -// mPhotoRaw = data; PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto); -// PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhotoRaw); mPicPreview.setId(R.id.pic_preview); mCameraLayout.addView(mPicPreview); @@ -145,7 +138,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL filterButtons.setVisibility(View.VISIBLE); filterButtons.bringToFront(); -// mPicPreview.invalidate(); switchButtons(view); } }); @@ -158,6 +150,7 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL view.findViewById(R.id.comment_box).setClickable(true); view.findViewById(R.id.comment_box).setVisibility(View.VISIBLE); view.findViewById(R.id.comment_box).bringToFront(); + ((FloatingActionButton)view.findViewById(R.id.upload_button)).hide(); } }); @@ -173,7 +166,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL upload.uploadPicture(new BitmapPost(((PicturePreview)view.findViewById(R.id.pic_preview)).getPicture(), comment)); mPhoto.recycle(); -// mPhoto = null; filterButtons.setVisibility(View.GONE); switchButtons(view); @@ -204,7 +196,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL PicturePreview.filterPrev(); PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto); -// PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhotoRaw); mPicPreview.setId(R.id.pic_preview); mCameraLayout.addView(mPicPreview); @@ -223,7 +214,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL PicturePreview.filterNext(); PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto); -// PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhotoRaw); mPicPreview.setId(R.id.pic_preview); mCameraLayout.addView(mPicPreview); @@ -279,20 +269,13 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL public void onDestroyView() { super.onDestroyView(); - int padding = 16; // 6 dps + int padding = 16; float scale = getResources().getDisplayMetrics().density; int dp = (int) (padding * scale + 0.5f); ((AppCompatActivity)getActivity()).getSupportActionBar().show(); getActivity().findViewById(R.id.content).setPadding(dp,dp,dp,dp); } - public static Bitmap rotate(Bitmap bmp, int degree) { - Matrix mtx = new Matrix(); - mtx.postRotate(degree); - - return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true); - } - public static Camera getCameraInstance(int facing) { Camera c = null; try { @@ -313,18 +296,15 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL public void switchButtons(View view) { RelativeLayout pictureButtons = (RelativeLayout) view.findViewById(R.id.picture_taken_buttons); FloatingActionButton upload = (FloatingActionButton) view.findViewById(R.id.upload_button); -// FloatingActionButton save = (FloatingActionButton) view.findViewById(R.id.save_button); if (((Integer)upload.getVisibility()).equals(View.VISIBLE)) { upload.hide(); -// save.hide(); view.findViewById(R.id.picture_button).setVisibility(View.VISIBLE); view.findViewById(R.id.switch_camera_button).setVisibility(View.VISIBLE); } else { pictureButtons.bringToFront(); upload.show(); -// save.show(); view.findViewById(R.id.picture_button).setVisibility(View.GONE); view.findViewById(R.id.switch_camera_button).setVisibility(View.GONE); diff --git a/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraPreview.java b/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraPreview.java index 0f07ad2..3e99389 100644 --- a/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraPreview.java +++ b/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/CameraPreview.java @@ -34,21 +34,14 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (mHolder.getSurface() == null){ - // preview surface does not exist return; } - // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e){ - // ignore: tried to stop a non-existent preview } - // set preview size and make any resize, rotate or - // reformatting changes here - - // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); @@ -60,6 +53,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback @Override public void surfaceDestroyed(SurfaceHolder holder) { + Log.d(TAG, "surfaceDestroyed: CAMERA DESTROYED"); mCamera.stopPreview(); mCamera.release(); } diff --git a/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/PicturePreview.java b/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/PicturePreview.java index a9364de..47a2a46 100644 --- a/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/PicturePreview.java +++ b/app/MyHyvesBookPlusStagram/app/src/main/java/nl/myhyvesbookplus/tagram/PicturePreview.java @@ -26,36 +26,20 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac private static int currentFilter = FILTER_NONE; - private BitmapFactory.Options options = new BitmapFactory.Options(); - private int imageHeight; - private int imageWidth; - Bitmap picture; Bitmap filterPicture; - byte[] data; public PicturePreview(Context context, Bitmap bmp) { -// public PicturePreview(Context context, byte[] data) { super(context); setWillNotDraw(false); -// this.data = data; -// options.inJustDecodeBounds = true; - picture = Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 4, bmp.getHeight() / 4, false); -// picture = BitmapFactory.decodeByteArray(data, 0, data.length, options); -// imageHeight = options.outHeight; -// imageWidth = options.outWidth; + picture = Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 2, bmp.getHeight() / 2, false); + Log.d(TAG, "PicturePreview: " + bmp.getWidth() + " " + bmp.getHeight()); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); -// options.inSampleSize = 4; //calculateInSampleSize(options, canvas.getWidth(), canvas.getHeight()); -// options.inJustDecodeBounds = false; -// picture = BitmapFactory.decodeByteArray(data, 0, data.length, options); -// picture = Bitmap.createBitmap(bmp); -// bmp.recycle(); - ColorMatrix cm = new ColorMatrix(); Paint paint = new Paint(); ColorMatrixColorFilter filter; @@ -63,12 +47,10 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac switch (currentFilter) { case FILTER_NONE: -// canvas.rotate(90); canvas.drawBitmap(rotate(picture, 90), 0, 0, null); filterPicture = rotate(picture, 90); break; case FILTER_SEPIA: -// filterPicture = Bitmap.createBitmap(picture.getWidth() / 4, picture.getHeight() / 4, Bitmap.Config.ARGB_8888); filterPicture = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); Log.d(TAG, "onDraw: " + Integer.toString(canvas.getWidth())); @@ -80,29 +62,21 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac filter = new ColorMatrixColorFilter(cm); paint.setColorFilter(filter); - saveCanvas.setBitmap(filterPicture); -// saveCanvas.rotate(90); -// canvas.rotate(90); saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint); canvas.drawBitmap(rotate(picture, 90), 0, 0, paint); break; case FILTER_BW: -// filterPicture = Bitmap.createBitmap(picture.getWidth() / 4, picture.getHeight() / 4, Bitmap.Config.ARGB_8888); filterPicture = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); -// filterPicture = Bitmap.createBitmap(1920, 1440, null); cm.setSaturation(0); filter = new ColorMatrixColorFilter(cm); paint.setColorFilter(filter); saveCanvas.setBitmap(filterPicture); -// saveCanvas.rotate(90); -// canvas.rotate(90); saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint); canvas.drawBitmap(rotate(picture, 90), 0, 0, paint); break; case FILTER_NEG: -// filterPicture = Bitmap.createBitmap(picture.getWidth() / 4, picture.getHeight() / 4, Bitmap.Config.ARGB_8888); filterPicture = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); float[] neg = {-1f,0f,0f,0f,255f, @@ -114,8 +88,6 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac filter = new ColorMatrixColorFilter(cm); paint.setColorFilter(filter); saveCanvas.setBitmap(filterPicture); -// saveCanvas.rotate(90); -// canvas.rotate(90); saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint); canvas.drawBitmap(rotate(picture, 90), 0, 0, paint); break; @@ -164,30 +136,8 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true); } - public static int calculateInSampleSize( - BitmapFactory.Options options, int reqWidth, int reqHeight) { - // Raw height and width of image - final int height = options.outHeight; - final int width = options.outWidth; - int inSampleSize = 1; - - if (height > reqHeight || width > reqWidth) { - - final int halfHeight = height / 2; - final int halfWidth = width / 2; - - // Calculate the largest inSampleSize value that is a power of 2 and keeps both - // height and width larger than the requested height and width. - while ((halfHeight / inSampleSize) >= reqHeight - && (halfWidth / inSampleSize) >= reqWidth) { - inSampleSize *= 2; - } - } - - return inSampleSize; - } - public Bitmap getPicture() { + picture.recycle(); return filterPicture; } @@ -201,5 +151,8 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac @Override public void surfaceDestroyed(SurfaceHolder holder) { + Log.d(TAG, "surfaceDestroyed: PICTURE DESTROYED"); + picture.recycle(); + filterPicture.recycle(); } }