Image preview working
This commit is contained in:
@@ -4,7 +4,6 @@ import android.app.Activity;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Matrix;
|
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.hardware.Camera.PictureCallback;
|
import android.hardware.Camera.PictureCallback;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@@ -46,7 +45,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private CameraPreview mPreview;
|
private CameraPreview mPreview;
|
||||||
private byte[] mPhotoRaw;
|
|
||||||
private Bitmap mPhoto;
|
private Bitmap mPhoto;
|
||||||
private int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
|
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 pictureButtons = (RelativeLayout) view.findViewById(R.id.picture_taken_buttons);
|
||||||
final RelativeLayout filterButtons = (RelativeLayout) view.findViewById(R.id.filter_buttons);
|
final RelativeLayout filterButtons = (RelativeLayout) view.findViewById(R.id.filter_buttons);
|
||||||
final RelativeLayout mCameraLayout = (RelativeLayout) view.findViewById(R.id.camera_preview);
|
final RelativeLayout mCameraLayout = (RelativeLayout) view.findViewById(R.id.camera_preview);
|
||||||
// final RelativeLayout mImageTaken = (RelativeLayout) view.findViewById(R.id.picture_view);
|
|
||||||
|
|
||||||
mCameraLayout.addView(mPreview);
|
mCameraLayout.addView(mPreview);
|
||||||
|
|
||||||
@@ -131,13 +128,9 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
mCamera.takePicture(null, null, new PictureCallback() {
|
mCamera.takePicture(null, null, new PictureCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onPictureTaken(byte[] data, Camera camera) {
|
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);
|
mPhoto = BitmapFactory.decodeByteArray(data, 0, data.length, null);
|
||||||
// mPhotoRaw = data;
|
|
||||||
|
|
||||||
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
|
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
|
||||||
// PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhotoRaw);
|
|
||||||
mPicPreview.setId(R.id.pic_preview);
|
mPicPreview.setId(R.id.pic_preview);
|
||||||
|
|
||||||
mCameraLayout.addView(mPicPreview);
|
mCameraLayout.addView(mPicPreview);
|
||||||
@@ -145,7 +138,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
filterButtons.setVisibility(View.VISIBLE);
|
filterButtons.setVisibility(View.VISIBLE);
|
||||||
filterButtons.bringToFront();
|
filterButtons.bringToFront();
|
||||||
|
|
||||||
// mPicPreview.invalidate();
|
|
||||||
switchButtons(view);
|
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).setClickable(true);
|
||||||
view.findViewById(R.id.comment_box).setVisibility(View.VISIBLE);
|
view.findViewById(R.id.comment_box).setVisibility(View.VISIBLE);
|
||||||
view.findViewById(R.id.comment_box).bringToFront();
|
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));
|
upload.uploadPicture(new BitmapPost(((PicturePreview)view.findViewById(R.id.pic_preview)).getPicture(), comment));
|
||||||
|
|
||||||
mPhoto.recycle();
|
mPhoto.recycle();
|
||||||
// mPhoto = null;
|
|
||||||
|
|
||||||
filterButtons.setVisibility(View.GONE);
|
filterButtons.setVisibility(View.GONE);
|
||||||
switchButtons(view);
|
switchButtons(view);
|
||||||
@@ -204,7 +196,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
PicturePreview.filterPrev();
|
PicturePreview.filterPrev();
|
||||||
|
|
||||||
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
|
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
|
||||||
// PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhotoRaw);
|
|
||||||
mPicPreview.setId(R.id.pic_preview);
|
mPicPreview.setId(R.id.pic_preview);
|
||||||
|
|
||||||
mCameraLayout.addView(mPicPreview);
|
mCameraLayout.addView(mPicPreview);
|
||||||
@@ -223,7 +214,6 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
PicturePreview.filterNext();
|
PicturePreview.filterNext();
|
||||||
|
|
||||||
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
|
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
|
||||||
// PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhotoRaw);
|
|
||||||
mPicPreview.setId(R.id.pic_preview);
|
mPicPreview.setId(R.id.pic_preview);
|
||||||
|
|
||||||
mCameraLayout.addView(mPicPreview);
|
mCameraLayout.addView(mPicPreview);
|
||||||
@@ -279,20 +269,13 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
|
|
||||||
int padding = 16; // 6 dps
|
int padding = 16;
|
||||||
float scale = getResources().getDisplayMetrics().density;
|
float scale = getResources().getDisplayMetrics().density;
|
||||||
int dp = (int) (padding * scale + 0.5f);
|
int dp = (int) (padding * scale + 0.5f);
|
||||||
((AppCompatActivity)getActivity()).getSupportActionBar().show();
|
((AppCompatActivity)getActivity()).getSupportActionBar().show();
|
||||||
getActivity().findViewById(R.id.content).setPadding(dp,dp,dp,dp);
|
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) {
|
public static Camera getCameraInstance(int facing) {
|
||||||
Camera c = null;
|
Camera c = null;
|
||||||
try {
|
try {
|
||||||
@@ -313,18 +296,15 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
|
|||||||
public void switchButtons(View view) {
|
public void switchButtons(View view) {
|
||||||
RelativeLayout pictureButtons = (RelativeLayout) view.findViewById(R.id.picture_taken_buttons);
|
RelativeLayout pictureButtons = (RelativeLayout) view.findViewById(R.id.picture_taken_buttons);
|
||||||
FloatingActionButton upload = (FloatingActionButton) view.findViewById(R.id.upload_button);
|
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)) {
|
if (((Integer)upload.getVisibility()).equals(View.VISIBLE)) {
|
||||||
upload.hide();
|
upload.hide();
|
||||||
// save.hide();
|
|
||||||
|
|
||||||
view.findViewById(R.id.picture_button).setVisibility(View.VISIBLE);
|
view.findViewById(R.id.picture_button).setVisibility(View.VISIBLE);
|
||||||
view.findViewById(R.id.switch_camera_button).setVisibility(View.VISIBLE);
|
view.findViewById(R.id.switch_camera_button).setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
pictureButtons.bringToFront();
|
pictureButtons.bringToFront();
|
||||||
upload.show();
|
upload.show();
|
||||||
// save.show();
|
|
||||||
|
|
||||||
view.findViewById(R.id.picture_button).setVisibility(View.GONE);
|
view.findViewById(R.id.picture_button).setVisibility(View.GONE);
|
||||||
view.findViewById(R.id.switch_camera_button).setVisibility(View.GONE);
|
view.findViewById(R.id.switch_camera_button).setVisibility(View.GONE);
|
||||||
|
|||||||
@@ -34,21 +34,14 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
|||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||||
if (mHolder.getSurface() == null){
|
if (mHolder.getSurface() == null){
|
||||||
// preview surface does not exist
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop preview before making changes
|
|
||||||
try {
|
try {
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
} catch (Exception e){
|
} 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 {
|
try {
|
||||||
mCamera.setPreviewDisplay(mHolder);
|
mCamera.setPreviewDisplay(mHolder);
|
||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
@@ -60,6 +53,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
Log.d(TAG, "surfaceDestroyed: CAMERA DESTROYED");
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
mCamera.release();
|
mCamera.release();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,36 +26,20 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
|
|||||||
|
|
||||||
private static int currentFilter = FILTER_NONE;
|
private static int currentFilter = FILTER_NONE;
|
||||||
|
|
||||||
private BitmapFactory.Options options = new BitmapFactory.Options();
|
|
||||||
private int imageHeight;
|
|
||||||
private int imageWidth;
|
|
||||||
|
|
||||||
Bitmap picture;
|
Bitmap picture;
|
||||||
Bitmap filterPicture;
|
Bitmap filterPicture;
|
||||||
byte[] data;
|
|
||||||
|
|
||||||
public PicturePreview(Context context, Bitmap bmp) {
|
public PicturePreview(Context context, Bitmap bmp) {
|
||||||
// public PicturePreview(Context context, byte[] data) {
|
|
||||||
super(context);
|
super(context);
|
||||||
setWillNotDraw(false);
|
setWillNotDraw(false);
|
||||||
|
|
||||||
// this.data = data;
|
picture = Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 2, bmp.getHeight() / 2, false);
|
||||||
// options.inJustDecodeBounds = true;
|
Log.d(TAG, "PicturePreview: " + bmp.getWidth() + " " + bmp.getHeight());
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(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();
|
ColorMatrix cm = new ColorMatrix();
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
ColorMatrixColorFilter filter;
|
ColorMatrixColorFilter filter;
|
||||||
@@ -63,12 +47,10 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
|
|||||||
|
|
||||||
switch (currentFilter) {
|
switch (currentFilter) {
|
||||||
case FILTER_NONE:
|
case FILTER_NONE:
|
||||||
// canvas.rotate(90);
|
|
||||||
canvas.drawBitmap(rotate(picture, 90), 0, 0, null);
|
canvas.drawBitmap(rotate(picture, 90), 0, 0, null);
|
||||||
filterPicture = rotate(picture, 90);
|
filterPicture = rotate(picture, 90);
|
||||||
break;
|
break;
|
||||||
case FILTER_SEPIA:
|
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);
|
filterPicture = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
|
||||||
Log.d(TAG, "onDraw: " + Integer.toString(canvas.getWidth()));
|
Log.d(TAG, "onDraw: " + Integer.toString(canvas.getWidth()));
|
||||||
|
|
||||||
@@ -80,29 +62,21 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
|
|||||||
|
|
||||||
filter = new ColorMatrixColorFilter(cm);
|
filter = new ColorMatrixColorFilter(cm);
|
||||||
paint.setColorFilter(filter);
|
paint.setColorFilter(filter);
|
||||||
saveCanvas.setBitmap(filterPicture);
|
|
||||||
// saveCanvas.rotate(90);
|
|
||||||
// canvas.rotate(90);
|
|
||||||
saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
||||||
canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
||||||
break;
|
break;
|
||||||
case FILTER_BW:
|
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(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
// filterPicture = Bitmap.createBitmap(1920, 1440, null);
|
|
||||||
cm.setSaturation(0);
|
cm.setSaturation(0);
|
||||||
|
|
||||||
filter = new ColorMatrixColorFilter(cm);
|
filter = new ColorMatrixColorFilter(cm);
|
||||||
paint.setColorFilter(filter);
|
paint.setColorFilter(filter);
|
||||||
saveCanvas.setBitmap(filterPicture);
|
saveCanvas.setBitmap(filterPicture);
|
||||||
// saveCanvas.rotate(90);
|
|
||||||
// canvas.rotate(90);
|
|
||||||
saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
||||||
canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
||||||
break;
|
break;
|
||||||
case FILTER_NEG:
|
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);
|
filterPicture = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
float[] neg = {-1f,0f,0f,0f,255f,
|
float[] neg = {-1f,0f,0f,0f,255f,
|
||||||
@@ -114,8 +88,6 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
|
|||||||
filter = new ColorMatrixColorFilter(cm);
|
filter = new ColorMatrixColorFilter(cm);
|
||||||
paint.setColorFilter(filter);
|
paint.setColorFilter(filter);
|
||||||
saveCanvas.setBitmap(filterPicture);
|
saveCanvas.setBitmap(filterPicture);
|
||||||
// saveCanvas.rotate(90);
|
|
||||||
// canvas.rotate(90);
|
|
||||||
saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
||||||
canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
|
||||||
break;
|
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);
|
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() {
|
public Bitmap getPicture() {
|
||||||
|
picture.recycle();
|
||||||
return filterPicture;
|
return filterPicture;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,5 +151,8 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
Log.d(TAG, "surfaceDestroyed: PICTURE DESTROYED");
|
||||||
|
picture.recycle();
|
||||||
|
filterPicture.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user