Camera preview working, with non working picture button
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package nl.myhyvesbookplus.tagram;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Camera;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.app.Fragment;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
/**
|
||||
* A simple {@link Fragment} subclass.
|
||||
@@ -29,6 +31,9 @@ public class CameraFragment extends Fragment {
|
||||
|
||||
private OnFragmentInteractionListener mListener;
|
||||
|
||||
private Camera mCamera;
|
||||
private CameraPreview mPreview;
|
||||
|
||||
public CameraFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
@@ -58,13 +63,28 @@ 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);
|
||||
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
|
||||
@@ -91,6 +111,17 @@ public class CameraFragment extends Fragment {
|
||||
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
|
||||
* fragment to allow an interaction in this fragment to be communicated
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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,17 +10,36 @@
|
||||
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"
|
||||
android:layout_weight="1">
|
||||
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
<ImageButton
|
||||
android:id="@+id/picture_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="logOutOnClick"
|
||||
android:text="LogOut" />
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user