Add negative filter + rotate half working

This commit is contained in:
Felix Atsma
2017-06-27 15:06:20 +02:00
parent 08849521e1
commit d3395f3e9b
2 changed files with 111 additions and 46 deletions

View File

@@ -134,8 +134,10 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
// Bitmap bmp = rotate(BitmapFactory.decodeByteArray(data, 0, data.length, null), 90); // Bitmap bmp = rotate(BitmapFactory.decodeByteArray(data, 0, data.length, null), 90);
// mPhoto = bmp; // 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);
@@ -171,7 +173,7 @@ 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; // mPhoto = null;
filterButtons.setVisibility(View.GONE); filterButtons.setVisibility(View.GONE);
switchButtons(view); switchButtons(view);
@@ -202,6 +204,7 @@ 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);
@@ -220,6 +223,7 @@ 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);
@@ -284,7 +288,7 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
public static Bitmap rotate(Bitmap bmp, int degree) { public static Bitmap rotate(Bitmap bmp, int degree) {
Matrix mtx = new Matrix(); Matrix mtx = new Matrix();
mtx.setRotate(degree); mtx.postRotate(degree);
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true); return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true);
} }

View File

@@ -2,10 +2,12 @@ package nl.myhyvesbookplus.tagram;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.graphics.ColorMatrix; import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter; import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint; import android.graphics.Paint;
import android.util.Log; import android.util.Log;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@@ -16,60 +18,115 @@ import android.view.SurfaceView;
*/ */
public class PicturePreview extends SurfaceView implements SurfaceHolder.Callback { public class PicturePreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "PicturePreveiew";
private static final int FILTER_NONE = 0; private static final int FILTER_NONE = 0;
private static final int FILTER_SEPIA = 1; private static final int FILTER_SEPIA = 1;
private static final int FILTER_BW = 2; private static final int FILTER_BW = 2;
private static final int FILTER_NEG = 3;
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);
picture = Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 2, bmp.getHeight() / 2, false);
// picture = Bitmap.createBitmap(bmp);
setWillNotDraw(false); 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;
} }
@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;
Canvas saveCanvas = new Canvas();
switch (currentFilter) { switch (currentFilter) {
case FILTER_NONE: case FILTER_NONE:
canvas.drawBitmap(picture, 0, 0, null); // canvas.rotate(90);
canvas.rotate(90); canvas.drawBitmap(rotate(picture, 90), 0, 0, null);
filterPicture = picture; filterPicture = rotate(picture, 90);
break; break;
case FILTER_SEPIA: case FILTER_SEPIA:
canvas.drawBitmap(toSepia(picture), 0, 0, null); // filterPicture = Bitmap.createBitmap(picture.getWidth() / 4, picture.getHeight() / 4, Bitmap.Config.ARGB_8888);
canvas.rotate(90); filterPicture = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
filterPicture = toSepia(picture); Log.d(TAG, "onDraw: " + Integer.toString(canvas.getWidth()));
break;
case FILTER_BW: float[] sepia = {0.393f,0.769f,0.189f,0f,0f,
Canvas bw = new Canvas(); 0.349f,0.686f,0.168f,0f,0f,
// filterPicture = Bitmap.createBitmap(1920, 1440, null); 0.272f,0.534f,0.131f,0f,0f,
filterPicture = Bitmap.createBitmap(picture.getWidth() / 2, picture.getHeight() / 2, Bitmap.Config.ARGB_8888); 0f, 0f, 0f, 1f, 0f};
cm.setSaturation(0); cm.set(sepia);
filter = new ColorMatrixColorFilter(cm); filter = new ColorMatrixColorFilter(cm);
paint.setColorFilter(filter); paint.setColorFilter(filter);
bw.setBitmap(filterPicture); saveCanvas.setBitmap(filterPicture);
bw.drawBitmap(picture, 0, 0, paint); // saveCanvas.rotate(90);
bw.rotate(90); // canvas.rotate(90);
canvas.drawBitmap(picture, 0, 0, paint); saveCanvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
canvas.rotate(90); canvas.drawBitmap(rotate(picture, 90), 0, 0, paint);
break; 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,
0f,-1f,0f,0f,255f,
0f,0f,-1f,0f,255f,
0f,0f,0f,1f,0f};
cm.set(neg);
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;
} }
} }
public static void filterPrev() { public static void filterPrev() {
switch (currentFilter) { switch (currentFilter) {
case FILTER_NONE: case FILTER_NONE:
currentFilter = FILTER_BW; currentFilter = FILTER_NEG;
break; break;
case FILTER_SEPIA: case FILTER_SEPIA:
currentFilter = FILTER_NONE; currentFilter = FILTER_NONE;
@@ -77,6 +134,9 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
case FILTER_BW: case FILTER_BW:
currentFilter = FILTER_SEPIA; currentFilter = FILTER_SEPIA;
break; break;
case FILTER_NEG:
currentFilter = FILTER_BW;
break;
} }
} }
@@ -89,41 +149,42 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
currentFilter = FILTER_BW; currentFilter = FILTER_BW;
break; break;
case FILTER_BW: case FILTER_BW:
currentFilter = FILTER_NEG;
break;
case FILTER_NEG:
currentFilter = FILTER_NONE; currentFilter = FILTER_NONE;
break; break;
} }
} }
public Bitmap toSepia(Bitmap color) { public static Bitmap rotate(Bitmap bmp, int degree) {
int red, green, blue, pixel; Matrix mtx = new Matrix();
int height = color.getHeight(); mtx.postRotate(degree);
int width = color.getWidth();
int depth = 20;
Bitmap sepia = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true);
}
int[] pixels = new int[width * height]; public static int calculateInSampleSize(
color.getPixels(pixels, 0, width, 0, 0, width, height); BitmapFactory.Options options, int reqWidth, int reqHeight) {
for (int i = 0; i < pixels.length; i++) { // Raw height and width of image
pixel = pixels[i]; final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
red = (pixel >> 16) & 0xFF; if (height > reqHeight || width > reqWidth) {
green = (pixel >> 8) & 0xFF;
blue = pixel & 0xFF;
red = green = blue = (red + green + blue) / 3; final int halfHeight = height / 2;
final int halfWidth = width / 2;
red += (depth * 2); // Calculate the largest inSampleSize value that is a power of 2 and keeps both
green += depth; // height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
if (red > 255) && (halfWidth / inSampleSize) >= reqWidth) {
red = 255; inSampleSize *= 2;
if (green > 255) }
green = 255;
pixels[i] = (0xFF << 24) | (red << 16) | (green << 8) | blue;
} }
sepia.setPixels(pixels, 0, width, 0, 0, width, height);
return sepia; return inSampleSize;
} }
public Bitmap getPicture() { public Bitmap getPicture() {