Camera switching working
This commit is contained in:
@@ -2,14 +2,24 @@ 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.
|
||||
@@ -72,11 +82,10 @@ public class CameraFragment extends Fragment {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View view = inflater.inflate(R.layout.fragment_camera, container, false);
|
||||
final 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);
|
||||
final RelativeLayout preview = (RelativeLayout) view.findViewById(R.id.camera_preview);
|
||||
|
||||
preview.addView(mPreview);
|
||||
|
||||
@@ -84,6 +93,48 @@ public class CameraFragment extends Fragment {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -111,16 +162,29 @@ public class CameraFragment extends Fragment {
|
||||
mListener = null;
|
||||
}
|
||||
|
||||
public static Camera getCameraInstance(){
|
||||
public static Camera getCameraInstance(int facing) {
|
||||
Camera c = null;
|
||||
try {
|
||||
c = Camera.open(0);
|
||||
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
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
package nl.myhyvesbookplus.tagram;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Camera;
|
||||
import android.util.Log;
|
||||
@@ -9,12 +8,13 @@ import android.view.SurfaceView;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
|
||||
SurfaceHolder mHolder;
|
||||
Camera mCamera;
|
||||
private SurfaceHolder mHolder;
|
||||
private Camera mCamera;
|
||||
private static int facing = 0;
|
||||
|
||||
public CameraPreview(Context context, Camera camera) {
|
||||
super(context);
|
||||
mCamera = camera;
|
||||
mCamera = camera.open(facing);
|
||||
mCamera.setDisplayOrientation(90);
|
||||
|
||||
mHolder = getHolder();
|
||||
@@ -23,8 +23,6 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder mHolder) {
|
||||
Log.d("camera", "ding");
|
||||
|
||||
try {
|
||||
mCamera.setPreviewDisplay(mHolder);
|
||||
mCamera.startPreview();
|
||||
@@ -65,4 +63,11 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -13,8 +13,7 @@
|
||||
<RelativeLayout
|
||||
android:id="@+id/camera_preview"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_weight="1">
|
||||
android:layout_height="fill_parent" >
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/picture_button"
|
||||
|
||||
Reference in New Issue
Block a user