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);
// 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);
@@ -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));
mPhoto.recycle();
mPhoto = null;
// mPhoto = null;
filterButtons.setVisibility(View.GONE);
switchButtons(view);
@@ -202,6 +204,7 @@ 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);
@@ -220,6 +223,7 @@ 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);
@@ -284,7 +288,7 @@ public class CameraFragment extends Fragment implements PostUploader.PostUploadL
public static Bitmap rotate(Bitmap bmp, int degree) {
Matrix mtx = new Matrix();
mtx.setRotate(degree);
mtx.postRotate(degree);
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
@@ -16,60 +18,115 @@ import android.view.SurfaceView;
*/
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_SEPIA = 1;
private static final int FILTER_BW = 2;
private static final int FILTER_NEG = 3;
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);
picture = Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 2, bmp.getHeight() / 2, false);
// picture = Bitmap.createBitmap(bmp);
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
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;
Canvas saveCanvas = new Canvas();
switch (currentFilter) {
case FILTER_NONE:
canvas.drawBitmap(picture, 0, 0, null);
canvas.rotate(90);
filterPicture = picture;
// canvas.rotate(90);
canvas.drawBitmap(rotate(picture, 90), 0, 0, null);
filterPicture = rotate(picture, 90);
break;
case FILTER_SEPIA:
canvas.drawBitmap(toSepia(picture), 0, 0, null);
canvas.rotate(90);
filterPicture = toSepia(picture);
break;
case FILTER_BW:
Canvas bw = new Canvas();
// filterPicture = Bitmap.createBitmap(1920, 1440, null);
filterPicture = Bitmap.createBitmap(picture.getWidth() / 2, picture.getHeight() / 2, Bitmap.Config.ARGB_8888);
cm.setSaturation(0);
// 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()));
float[] sepia = {0.393f,0.769f,0.189f,0f,0f,
0.349f,0.686f,0.168f,0f,0f,
0.272f,0.534f,0.131f,0f,0f,
0f, 0f, 0f, 1f, 0f};
cm.set(sepia);
filter = new ColorMatrixColorFilter(cm);
paint.setColorFilter(filter);
bw.setBitmap(filterPicture);
bw.drawBitmap(picture, 0, 0, paint);
bw.rotate(90);
canvas.drawBitmap(picture, 0, 0, paint);
canvas.rotate(90);
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,
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() {
switch (currentFilter) {
case FILTER_NONE:
currentFilter = FILTER_BW;
currentFilter = FILTER_NEG;
break;
case FILTER_SEPIA:
currentFilter = FILTER_NONE;
@@ -77,6 +134,9 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
case FILTER_BW:
currentFilter = FILTER_SEPIA;
break;
case FILTER_NEG:
currentFilter = FILTER_BW;
break;
}
}
@@ -89,41 +149,42 @@ public class PicturePreview extends SurfaceView implements SurfaceHolder.Callbac
currentFilter = FILTER_BW;
break;
case FILTER_BW:
currentFilter = FILTER_NEG;
break;
case FILTER_NEG:
currentFilter = FILTER_NONE;
break;
}
}
public Bitmap toSepia(Bitmap color) {
int red, green, blue, pixel;
int height = color.getHeight();
int width = color.getWidth();
int depth = 20;
public static Bitmap rotate(Bitmap bmp, int degree) {
Matrix mtx = new Matrix();
mtx.postRotate(degree);
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];
color.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < pixels.length; i++) {
pixel = pixels[i];
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;
red = (pixel >> 16) & 0xFF;
green = (pixel >> 8) & 0xFF;
blue = pixel & 0xFF;
if (height > reqHeight || width > reqWidth) {
red = green = blue = (red + green + blue) / 3;
final int halfHeight = height / 2;
final int halfWidth = width / 2;
red += (depth * 2);
green += depth;
if (red > 255)
red = 255;
if (green > 255)
green = 255;
pixels[i] = (0xFF << 24) | (red << 16) | (green << 8) | blue;
// 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;
}
}
sepia.setPixels(pixels, 0, width, 0, 0, width, height);
return sepia;
return inSampleSize;
}
public Bitmap getPicture() {