Merge branch 'felix' into 'master'

Camera filter switching and uploading

See merge request !12
This commit was merged in pull request #12.
This commit is contained in:
Felix Atsma
2017-06-26 10:51:41 +02:00
9 changed files with 388 additions and 74 deletions

View File

@@ -1,25 +1,22 @@
package nl.myhyvesbookplus.tagram; package nl.myhyvesbookplus.tagram;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
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;
import android.os.Bundle; import android.os.Bundle;
import android.app.Fragment; import android.app.Fragment;
import android.support.v7.app.AppCompatActivity; import android.support.design.widget.FloatingActionButton;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import java.io.File; import nl.myhyvesbookplus.tagram.controller.UploadClass;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
@@ -31,6 +28,7 @@ import java.io.IOException;
*/ */
public class CameraFragment extends Fragment { public class CameraFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match // TODO: Rename parameter arguments, choose names that match
private static final String TAG = "CameraFragment";
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2"; private static final String ARG_PARAM2 = "param2";
@@ -43,6 +41,9 @@ public class CameraFragment extends Fragment {
private Camera mCamera; private Camera mCamera;
private CameraPreview mPreview; private CameraPreview mPreview;
private byte[] mPhotoRaw;
private Bitmap mPhoto;
private int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
public CameraFragment() { public CameraFragment() {
// Required empty public constructor // Required empty public constructor
@@ -73,9 +74,6 @@ public class CameraFragment extends Fragment {
mParam1 = getArguments().getString(ARG_PARAM1); mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2); mParam2 = getArguments().getString(ARG_PARAM2);
} }
// Hide top bar
// ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
} }
@Override @Override
@@ -84,54 +82,127 @@ public class CameraFragment extends Fragment {
// Inflate the layout for this fragment // Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_camera, container, false); final View view = inflater.inflate(R.layout.fragment_camera, container, false);
mCamera = getCameraInstance(facing);
Camera.Parameters params = mCamera.getParameters();
params.setRotation(0);
mCamera.setParameters(params);
mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera); mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
final RelativeLayout preview = (RelativeLayout) view.findViewById(R.id.camera_preview); 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);
preview.addView(mPreview); mCameraLayout.addView(mPreview);
// Draw picture and switch button over preview // Draw buttons over preview
view.findViewById(R.id.picture_button).bringToFront(); view.findViewById(R.id.picture_button).bringToFront();
view.findViewById(R.id.switch_camera_button).bringToFront(); view.findViewById(R.id.switch_camera_button).bringToFront();
pictureButtons.bringToFront();
filterButtons.bringToFront();
((ImageButton)view.findViewById(R.id.picture_button)).setOnClickListener(new View.OnClickListener() { (view.findViewById(R.id.switch_camera_button)).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Toast.makeText(getActivity(), "Snap!", Toast.LENGTH_SHORT).show(); switchFacing();
/*
PictureCallback mPicture = new PictureCallback() {
@Override mCameraLayout.removeView(mPreview);
public void onPictureTaken(byte[] data, Camera camera) { mCamera = getCameraInstance(facing);
Log.v("picture", "Getting output media file"); mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
File pictureFile = getOutputMediaFile(); mCameraLayout.addView(mPreview);
if (pictureFile == null) {
Log.v("picture", "Error creating output file"); view.findViewById(R.id.picture_button).bringToFront();
return; view.findViewById(R.id.switch_camera_button).bringToFront();
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (IOException e) {
Log.v("picture", e.getMessage());
}
}
};
*/
} }
}); });
((ImageButton)view.findViewById(R.id.switch_camera_button)).setOnClickListener(new View.OnClickListener() { (view.findViewById(R.id.picture_button)).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
CameraPreview.switchFacing(); 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;
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
mPicPreview.setId(R.id.pic_preview);
Log.d(TAG, "onPictureTaken: PICTURE");
mCameraLayout.addView(mPicPreview);
filterButtons.setVisibility(View.VISIBLE);
filterButtons.bringToFront();
// mPicPreview.invalidate();
switchButtons(view);
}
});
}
});
(view.findViewById(R.id.upload_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UploadClass upload = new UploadClass();
upload.uploadPicture(mPhoto);
mPhoto.recycle();
mPhoto = null;
filterButtons.setVisibility(View.GONE);
switchButtons(view);
mCameraLayout.removeView(mPreview);
mCamera = getCameraInstance(facing);
Camera.Parameters params = mCamera.getParameters();
params.setRotation(0);
mCamera.setParameters(params);
preview.removeView(mPreview);
mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera); mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
preview.addView(mPreview); mCameraLayout.addView(mPreview);
view.findViewById(R.id.picture_button).bringToFront(); view.findViewById(R.id.picture_button).bringToFront();
view.findViewById(R.id.switch_camera_button).bringToFront(); view.findViewById(R.id.switch_camera_button).bringToFront();
mCameraLayout.removeView(view.findViewById(R.id.pic_preview));
}
});
(view.findViewById(R.id.filter_left)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCameraLayout.removeView(view.findViewById(R.id.pic_preview));
PicturePreview.filterPrev();
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
mPicPreview.setId(R.id.pic_preview);
mCameraLayout.addView(mPicPreview);
view.findViewById(R.id.picture_taken_buttons).bringToFront();
filterButtons.setVisibility(View.VISIBLE);
filterButtons.bringToFront();
}
});
(view.findViewById(R.id.filter_right)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCameraLayout.removeView(view.findViewById(R.id.pic_preview));
PicturePreview.filterNext();
PicturePreview mPicPreview = new PicturePreview(getActivity().getBaseContext(), mPhoto);
mPicPreview.setId(R.id.pic_preview);
mCameraLayout.addView(mPicPreview);
view.findViewById(R.id.picture_taken_buttons).bringToFront();
filterButtons.setVisibility(View.VISIBLE);
filterButtons.bringToFront();
} }
}); });
@@ -162,6 +233,16 @@ public class CameraFragment extends Fragment {
mListener = null; mListener = null;
} }
public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
public static Camera getCameraInstance(int facing) { public static Camera getCameraInstance(int facing) {
Camera c = null; Camera c = null;
try { try {
@@ -172,6 +253,34 @@ public class CameraFragment extends Fragment {
return c; return c;
} }
public void switchFacing() {
if (facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
facing = Camera.CameraInfo.CAMERA_FACING_BACK;
else
facing = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
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);
}
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
@@ -182,10 +291,6 @@ public class CameraFragment extends Fragment {
super.onResume(); super.onResume();
} }
public void setCamera(Camera c) {
this.mCamera = c;
}
/** /**
* This interface must be implemented by activities that contain this * This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated * fragment to allow an interaction in this fragment to be communicated

View File

@@ -8,13 +8,14 @@ import android.view.SurfaceView;
import java.io.IOException; import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static String TAG = "CameraPreview";
private SurfaceHolder mHolder; private SurfaceHolder mHolder;
private Camera mCamera; private Camera mCamera;
private static int facing = 0; private static int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
public CameraPreview(Context context, Camera camera) { public CameraPreview(Context context, Camera camera) {
super(context); super(context);
mCamera = camera.open(facing); mCamera = camera;
mCamera.setDisplayOrientation(90); mCamera.setDisplayOrientation(90);
mHolder = getHolder(); mHolder = getHolder();
@@ -24,6 +25,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
@Override @Override
public void surfaceCreated(SurfaceHolder mHolder) { public void surfaceCreated(SurfaceHolder mHolder) {
try { try {
Log.d(TAG, "surfaceCreated: CREATED");
mCamera.setPreviewDisplay(mHolder); mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview(); mCamera.startPreview();
} catch (IOException e) { } catch (IOException e) {
@@ -60,6 +62,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: DESTROYED");
mCamera.stopPreview(); mCamera.stopPreview();
mCamera.release(); mCamera.release();
} }

View File

@@ -0,0 +1,134 @@
package nl.myhyvesbookplus.tagram;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by felix on 23/06/2017.
*/
public class PicturePreview extends SurfaceView implements SurfaceHolder.Callback {
private static final int FILTER_NONE = 0;
private static final int FILTER_SEPIA = 1;
private static final int FILTER_BW = 2;
private static int currentFilter = FILTER_NONE;
Bitmap picture;
Bitmap filterPicture;
public PicturePreview(Context context, Bitmap bmp) {
super(context);
picture = Bitmap.createScaledBitmap(bmp, 1920, 1440, false);
setWillNotDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
ColorMatrix cm = new ColorMatrix();
Paint paint = new Paint();
ColorMatrixColorFilter filter;
switch (currentFilter) {
case FILTER_NONE:
canvas.drawBitmap(picture, 0, 0, null);
filterPicture = picture;
break;
case FILTER_SEPIA:
canvas.drawBitmap(toSepia(picture), 0, 0, null);
filterPicture = toSepia(picture);
break;
case FILTER_BW:
cm.setSaturation(0);
filter = new ColorMatrixColorFilter(cm);
paint.setColorFilter(filter);
canvas.drawBitmap(picture, 0, 0, paint);
break;
}
}
public static void filterPrev() {
switch (currentFilter) {
case FILTER_NONE:
currentFilter = FILTER_BW;
break;
case FILTER_SEPIA:
currentFilter = FILTER_NONE;
break;
case FILTER_BW:
currentFilter = FILTER_SEPIA;
break;
}
}
public static void filterNext() {
switch (currentFilter) {
case FILTER_NONE:
currentFilter = FILTER_SEPIA;
break;
case FILTER_SEPIA:
currentFilter = FILTER_BW;
break;
case FILTER_BW:
currentFilter = FILTER_NONE;
break;
}
}
public Bitmap toSepia(Bitmap color) {
int red, green, blue, pixel, gry;
int height = color.getHeight();
int width = color.getWidth();
int depth = 20;
Bitmap sepia = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
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];
red = (pixel >> 16) & 0xFF;
green = (pixel >> 8) & 0xFF;
blue = pixel & 0xFF;
red = green = blue = (red + green + blue) / 3;
red += (depth * 2);
green += depth;
if (red > 255)
red = 255;
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;
}
public Bitmap getPicture() {
return filterPicture;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 B

View File

@@ -5,15 +5,10 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="nl.myhyvesbookplus.tagram.CameraFragment"> tools:context="nl.myhyvesbookplus.tagram.CameraFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout <RelativeLayout
android:id="@+id/camera_preview" android:id="@+id/camera_preview"
android:layout_width="fill_parent" android:layout_width="match_parent"
android:layout_height="fill_parent" > android:layout_height="match_parent" >
<ImageButton <ImageButton
android:id="@+id/picture_button" android:id="@+id/picture_button"
@@ -24,6 +19,7 @@
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:layout_margin="10dp" android:layout_margin="10dp"
android:padding="10dp" android:padding="10dp"
android:scaleType="center"
android:scaleX="2" android:scaleX="2"
android:scaleY="2" android:scaleY="2"
app:srcCompat="@android:drawable/ic_menu_camera" /> app:srcCompat="@android:drawable/ic_menu_camera" />
@@ -37,8 +33,75 @@
android:background="@android:color/transparent" android:background="@android:color/transparent"
app:srcCompat="@android:drawable/ic_menu_revert" /> app:srcCompat="@android:drawable/ic_menu_revert" />
<ProgressBar
android:id="@+id/load_bar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerInParent="true"/>
<RelativeLayout
android:id="@+id/filter_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginBottom="60dp"
android:layout_centerVertical="true">
<ImageButton
android:id="@+id/filter_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:scaleX="0.5"
android:scaleY="0.5"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_arrow_back_black_24dp"/>
<ImageButton
android:id="@+id/filter_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:scaleX="0.5"
android:scaleY="0.5"
android:layout_alignParentRight="true"
android:src="@drawable/ic_arrow_forward_black_24dp"/>
</RelativeLayout> </RelativeLayout>
</LinearLayout> <RelativeLayout
android:id="@+id/picture_taken_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp">
<android.support.design.widget.FloatingActionButton
android:id="@+id/upload_button"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:scaleType="center"
app:fabSize="normal"
android:src="@android:drawable/ic_menu_upload"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/save_button"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:scaleType="center"
app:fabSize="normal"
android:src="@android:drawable/ic_menu_save"/>
</RelativeLayout>
</RelativeLayout>
</FrameLayout> </FrameLayout>

View File

@@ -2,5 +2,6 @@
<resources> <resources>
<color name="colorPrimary">#fbbf2d</color> <color name="colorPrimary">#fbbf2d</color>
<color name="colorPrimaryDark">#ffa70f</color> <color name="colorPrimaryDark">#ffa70f</color>
<color name="colorAccent">#4CAF50</color> <color name="colorAccent">#4bAfe5</color>
<!--<color name="colorAccent">#4CAF50</color>-->
</resources> </resources>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="pic_preview" type="id"/>
<item name="camera_preview" type="id"/>
</resources>

View File

@@ -24,4 +24,7 @@
<string name="profile_picture_description">profile picture</string> <string name="profile_picture_description">profile picture</string>
<string name="change_psw_button">Change Password</string> <string name="change_psw_button">Change Password</string>
<string name="please_wait">Please Wait</string> <string name="please_wait">Please Wait</string>
<string name="upload">Upload</string>
<string name="save">Save</string>
</resources> </resources>