mirror of
				https://github.com/SimpleMobileTools/Simple-Camera.git
				synced 2025-06-27 09:02:59 +02:00 
			
		
		
		
	add a button for toggling front/rear camera
This commit is contained in:
		| @@ -3,8 +3,8 @@ package com.simplemobiletools.camera; | ||||
| import android.hardware.Camera; | ||||
| import android.os.Bundle; | ||||
| import android.support.v7.app.AppCompatActivity; | ||||
| import android.util.Log; | ||||
| import android.view.SurfaceView; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.view.Window; | ||||
| import android.view.WindowManager; | ||||
| @@ -16,9 +16,10 @@ import butterknife.OnClick; | ||||
|  | ||||
| public class MainActivity extends AppCompatActivity { | ||||
|     @Bind(R.id.viewHolder) RelativeLayout viewHolder; | ||||
|     @Bind(R.id.toggle_camera) View toggleCameraBtn; | ||||
|  | ||||
|     private static final String TAG = Preview.class.getSimpleName(); | ||||
|     private Preview preview; | ||||
|     private int currCamera; | ||||
|  | ||||
|     @Override | ||||
|     protected void onCreate(Bundle savedInstanceState) { | ||||
| @@ -28,12 +29,17 @@ public class MainActivity extends AppCompatActivity { | ||||
|  | ||||
|         setContentView(R.layout.activity_main); | ||||
|         ButterKnife.bind(this); | ||||
|  | ||||
|         currCamera = Camera.CameraInfo.CAMERA_FACING_BACK; | ||||
|         preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView)); | ||||
|         preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); | ||||
|         viewHolder.addView(preview); | ||||
|     } | ||||
|  | ||||
|     @OnClick(R.id.toggle_camera) | ||||
|     public void toggleCamera() { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @OnClick(R.id.shutter) | ||||
|     public void takePicture() { | ||||
|         preview.takePicture(); | ||||
| @@ -43,12 +49,12 @@ public class MainActivity extends AppCompatActivity { | ||||
|     protected void onResume() { | ||||
|         super.onResume(); | ||||
|  | ||||
|         try { | ||||
|             Camera camera = Camera.open(0); | ||||
|             preview.setCamera(camera); | ||||
|         } catch (Exception e) { | ||||
|             Log.e(TAG, "onResume IOException " + e.getMessage()); | ||||
|         final int cnt = Camera.getNumberOfCameras(); | ||||
|         if (cnt == 1) { | ||||
|             toggleCameraBtn.setVisibility(View.INVISIBLE); | ||||
|         } | ||||
|         Camera camera = Camera.open(currCamera); | ||||
|         preview.setCamera(camera); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -39,7 +39,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O | ||||
|         surfaceHolder.addCallback(this); | ||||
|         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | ||||
|         canTakePicture = true; | ||||
|         setOnTouchListener(this); | ||||
|         surfaceView.setOnTouchListener(this); | ||||
|     } | ||||
|  | ||||
|     public void setCamera(Camera newCamera) { | ||||
|   | ||||
| @@ -10,13 +10,35 @@ | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content"/> | ||||
|  | ||||
|     <ImageView | ||||
|         android:id="@+id/shutter" | ||||
|         android:layout_width="wrap_content" | ||||
|     <LinearLayout | ||||
|         android:id="@+id/btn_holder" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_alignParentBottom="true" | ||||
|         android:layout_centerHorizontal="true" | ||||
|         android:layout_marginBottom="@dimen/preview_btn_margin" | ||||
|         android:src="@mipmap/ic_launcher"/> | ||||
|         android:gravity="center_horizontal"> | ||||
|  | ||||
|         <ImageView | ||||
|             android:id="@+id/toggle_camera" | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:src="@mipmap/ic_launcher"/> | ||||
|  | ||||
|         <ImageView | ||||
|             android:id="@+id/shutter" | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:src="@mipmap/ic_launcher"/> | ||||
|  | ||||
|         <ImageView | ||||
|             android:id="@+id/toggle_flash" | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:src="@mipmap/ic_launcher" | ||||
|             android:visibility="invisible"/> | ||||
|  | ||||
|     </LinearLayout> | ||||
| </RelativeLayout> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user