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.hardware.Camera; | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| import android.support.v7.app.AppCompatActivity; | import android.support.v7.app.AppCompatActivity; | ||||||
| import android.util.Log; |  | ||||||
| import android.view.SurfaceView; | import android.view.SurfaceView; | ||||||
|  | import android.view.View; | ||||||
| import android.view.ViewGroup; | import android.view.ViewGroup; | ||||||
| import android.view.Window; | import android.view.Window; | ||||||
| import android.view.WindowManager; | import android.view.WindowManager; | ||||||
| @@ -16,9 +16,10 @@ import butterknife.OnClick; | |||||||
|  |  | ||||||
| public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||||||
|     @Bind(R.id.viewHolder) RelativeLayout viewHolder; |     @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 Preview preview; | ||||||
|  |     private int currCamera; | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     protected void onCreate(Bundle savedInstanceState) { |     protected void onCreate(Bundle savedInstanceState) { | ||||||
| @@ -28,12 +29,17 @@ public class MainActivity extends AppCompatActivity { | |||||||
|  |  | ||||||
|         setContentView(R.layout.activity_main); |         setContentView(R.layout.activity_main); | ||||||
|         ButterKnife.bind(this); |         ButterKnife.bind(this); | ||||||
|  |         currCamera = Camera.CameraInfo.CAMERA_FACING_BACK; | ||||||
|         preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView)); |         preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView)); | ||||||
|         preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); |         preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); | ||||||
|         viewHolder.addView(preview); |         viewHolder.addView(preview); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @OnClick(R.id.toggle_camera) | ||||||
|  |     public void toggleCamera() { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @OnClick(R.id.shutter) |     @OnClick(R.id.shutter) | ||||||
|     public void takePicture() { |     public void takePicture() { | ||||||
|         preview.takePicture(); |         preview.takePicture(); | ||||||
| @@ -43,12 +49,12 @@ public class MainActivity extends AppCompatActivity { | |||||||
|     protected void onResume() { |     protected void onResume() { | ||||||
|         super.onResume(); |         super.onResume(); | ||||||
|  |  | ||||||
|         try { |         final int cnt = Camera.getNumberOfCameras(); | ||||||
|             Camera camera = Camera.open(0); |         if (cnt == 1) { | ||||||
|             preview.setCamera(camera); |             toggleCameraBtn.setVisibility(View.INVISIBLE); | ||||||
|         } catch (Exception e) { |  | ||||||
|             Log.e(TAG, "onResume IOException " + e.getMessage()); |  | ||||||
|         } |         } | ||||||
|  |         Camera camera = Camera.open(currCamera); | ||||||
|  |         preview.setCamera(camera); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|   | |||||||
| @@ -39,7 +39,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O | |||||||
|         surfaceHolder.addCallback(this); |         surfaceHolder.addCallback(this); | ||||||
|         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | ||||||
|         canTakePicture = true; |         canTakePicture = true; | ||||||
|         setOnTouchListener(this); |         surfaceView.setOnTouchListener(this); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void setCamera(Camera newCamera) { |     public void setCamera(Camera newCamera) { | ||||||
|   | |||||||
| @@ -10,13 +10,35 @@ | |||||||
|         android:layout_width="wrap_content" |         android:layout_width="wrap_content" | ||||||
|         android:layout_height="wrap_content"/> |         android:layout_height="wrap_content"/> | ||||||
|  |  | ||||||
|     <ImageView |     <LinearLayout | ||||||
|         android:id="@+id/shutter" |         android:id="@+id/btn_holder" | ||||||
|         android:layout_width="wrap_content" |         android:layout_width="match_parent" | ||||||
|         android:layout_height="wrap_content" |         android:layout_height="wrap_content" | ||||||
|         android:layout_alignParentBottom="true" |         android:layout_alignParentBottom="true" | ||||||
|         android:layout_centerHorizontal="true" |  | ||||||
|         android:layout_marginBottom="@dimen/preview_btn_margin" |         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> | </RelativeLayout> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user