Merge branch 'felix' into 'master'

Felix camera

See merge request !8
This commit was merged in pull request #8.
This commit is contained in:
Felix Atsma
2017-06-22 11:40:27 +02:00
5 changed files with 201 additions and 6 deletions

View File

@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.myhyvesbookplus.tagram">
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@@ -1,13 +1,25 @@
package nl.myhyvesbookplus.tagram;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* A simple {@link Fragment} subclass.
@@ -29,6 +41,9 @@ public class CameraFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private Camera mCamera;
private CameraPreview mPreview;
public CameraFragment() {
// Required empty public constructor
}
@@ -58,13 +73,69 @@ public class CameraFragment extends Fragment {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
// Hide top bar
// ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_camera, container, false);
final View view = inflater.inflate(R.layout.fragment_camera, container, false);
mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
final RelativeLayout preview = (RelativeLayout) view.findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Draw picture and switch button over preview
view.findViewById(R.id.picture_button).bringToFront();
view.findViewById(R.id.switch_camera_button).bringToFront();
((ImageButton)view.findViewById(R.id.picture_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Snap!", Toast.LENGTH_SHORT).show();
/*
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.v("picture", "Getting output media file");
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.v("picture", "Error creating output file");
return;
}
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() {
@Override
public void onClick(View v) {
CameraPreview.switchFacing();
preview.removeView(mPreview);
mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
preview.addView(mPreview);
view.findViewById(R.id.picture_button).bringToFront();
view.findViewById(R.id.switch_camera_button).bringToFront();
}
});
return view;
}
// TODO: Rename method, update argument and hook method into UI event
@@ -91,6 +162,30 @@ public class CameraFragment extends Fragment {
mListener = null;
}
public static Camera getCameraInstance(int facing) {
Camera c = null;
try {
c = Camera.open(facing);
} catch (Exception e) {
e.getStackTrace();
}
return c;
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
public void setCamera(Camera c) {
this.mCamera = c;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated

View File

@@ -0,0 +1,73 @@
package nl.myhyvesbookplus.tagram;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private static int facing = 0;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera.open(facing);
mCamera.setDisplayOrientation(90);
mHolder = getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder mHolder) {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
e.getStackTrace();
}
}
@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();
} catch (Exception e){
Log.d("camera", "Error starting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
public static void switchFacing() {
if (facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
facing = Camera.CameraInfo.CAMERA_FACING_BACK;
else
facing = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
}

View File

@@ -3,6 +3,7 @@ package nl.myhyvesbookplus.tagram;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;

View File

@@ -1,4 +1,5 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -9,12 +10,35 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_camera" />
<RelativeLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="@+id/picture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:layout_margin="10dp"
android:padding="10dp"
android:scaleX="2"
android:scaleY="2"
app:srcCompat="@android:drawable/ic_menu_camera" />
<ImageButton
android:id="@+id/switch_camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="9dp"
android:background="@android:color/transparent"
app:srcCompat="@android:drawable/ic_menu_revert" />
</RelativeLayout>
</LinearLayout>
<!-- TODO: Update blank fragment layout -->
</FrameLayout>