Created
May 9, 2012 15:11
-
-
Save CVirus/2645320 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import android.content.Context; | |
import android.hardware.Camera; | |
import android.util.Log; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
/** A basic Camera preview class */ | |
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { | |
private static final String TAG = "Camera Preview"; | |
private SurfaceHolder mHolder; | |
private Camera mCamera; | |
public CameraPreview(Context context, Camera camera) { | |
super(context); | |
mCamera = camera; | |
// Install a SurfaceHolder.Callback so we get notified when the | |
// underlying surface is created and destroyed. | |
mHolder = getHolder(); | |
mHolder.addCallback(this); | |
// deprecated setting, but required on Android versions prior to 3.0 | |
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); | |
} | |
public void surfaceCreated(SurfaceHolder holder) { | |
// The Surface has been created, now tell the camera where to draw the preview. | |
try { | |
mCamera.setPreviewDisplay(holder); | |
mCamera.startPreview(); | |
} catch (IOException e) { | |
Log.d(TAG, "Error setting camera preview: " + e.getMessage()); | |
} | |
} | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
// empty. Take care of releasing the Camera preview in your activity. | |
} | |
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { | |
// If your preview can change or rotate, take care of those events here. | |
// Make sure to stop the preview before resizing or reformatting it. | |
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(TAG, "Error starting camera preview: " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal" > | |
<Button | |
android:id="@+id/button_log" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_alignParentRight="true" | |
android:onClick="logSensorData" | |
android:text="@string/button_log_str" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.hardware.Camera; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
import android.widget.FrameLayout; | |
import android.widget.RelativeLayout; | |
import android.widget.TextSwitcher; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class Experiment1Activity extends Activity { | |
private Camera mCamera; | |
private CameraPreview mPreview; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Hide the window title. | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
setContentView(R.layout.main); | |
// Create an instance of Camera | |
mCamera = getCameraInstance(); | |
// Create our Preview view and set it as the content of our activity. | |
mPreview = new CameraPreview(this, mCamera); | |
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.camera_preview); | |
frameLayout.addView(mPreview); | |
RelativeLayout relativeLayoutControls = (RelativeLayout) findViewById(R.id.controls_layout); | |
relativeLayoutControls.bringToFront(); | |
RelativeLayout relativeLayoutSensorsData = (RelativeLayout) findViewById(R.id.sensors_data_layout); | |
relativeLayoutSensorsData.bringToFront(); | |
} | |
public void logSensorData(View view) | |
{ | |
Toast.makeText(getApplicationContext(), "Logged", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
protected void onPause() | |
{ | |
super.onPause(); | |
releaseCamera(); | |
} | |
/** A safe way to get an instance of the Camera object. */ | |
public static Camera getCameraInstance() { | |
Camera c = null; | |
try { | |
c = Camera.open(); | |
} | |
catch (Exception e){ | |
// Camera is not available (in use or does not exist) | |
} | |
return c; // returns null if camera is unavailable | |
} | |
private void releaseCamera(){ | |
if (mCamera != null){ | |
mCamera.release(); // release the camera for other applications | |
mCamera = null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/camera_preview" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:layout_weight="1" | |
android:orientation="horizontal" > | |
<include | |
android:id="@+id/controls_layout" | |
layout="@layout/controls" /> | |
<include | |
android:id="@+id/sensors_data_layout" | |
layout="@layout/sensors_data" /> | |
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<TextView | |
android:id="@+id/Xc" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentTop="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="20dp" | |
android:text="@string/textview_xc_str" | |
android:textColor="#FF0000" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment