Camera preview working, with non working picture button

This commit is contained in:
Felix Atsma
2017-06-20 21:20:54 +02:00
parent c05737575a
commit 1e5239921a
4 changed files with 133 additions and 12 deletions

View File

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

View File

@@ -1,13 +1,15 @@
package nl.myhyvesbookplus.tagram; package nl.myhyvesbookplus.tagram;
import android.content.Context; import android.content.Context;
import android.hardware.Camera;
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.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.RelativeLayout;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
@@ -29,6 +31,9 @@ public class CameraFragment extends Fragment {
private OnFragmentInteractionListener mListener; private OnFragmentInteractionListener mListener;
private Camera mCamera;
private CameraPreview mPreview;
public CameraFragment() { public CameraFragment() {
// Required empty public constructor // Required empty public constructor
} }
@@ -58,13 +63,28 @@ 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
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_camera, container, false); View view = inflater.inflate(R.layout.fragment_camera, container, false);
mCamera = getCameraInstance();
mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
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();
return view;
} }
// TODO: Rename method, update argument and hook method into UI event // TODO: Rename method, update argument and hook method into UI event
@@ -91,6 +111,17 @@ public class CameraFragment extends Fragment {
mListener = null; mListener = null;
} }
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(0);
} catch (Exception e) {
e.getStackTrace();
}
return 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

@@ -0,0 +1,68 @@
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 {
SurfaceHolder mHolder;
Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mCamera.setDisplayOrientation(90);
mHolder = getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder mHolder) {
Log.d("camera", "ding");
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();
}
}

View File

@@ -1,4 +1,5 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <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" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -9,17 +10,36 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<TextView <RelativeLayout
android:layout_width="match_parent" android:id="@+id/camera_preview"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="@string/hello_camera" /> android:layout_height="fill_parent"
android:layout_weight="1">
<Button <ImageButton
android:layout_width="match_parent" android:id="@+id/picture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="logOutOnClick" android:layout_alignParentBottom="true"
android:text="LogOut" /> 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> </LinearLayout>
<!-- TODO: Update blank fragment layout -->
</FrameLayout> </FrameLayout>