add Camera open / release

This commit is contained in:
tibbi 2016-04-13 20:44:03 +02:00
parent e8608713b6
commit b66ecf8769
2 changed files with 46 additions and 3 deletions

View File

@ -2,13 +2,21 @@
<manifest package="com.simplemobiletools.camera"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature
android:name="android.hardware.camera"
android:required="true"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -16,5 +24,4 @@
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -1,13 +1,49 @@
package com.simplemobiletools.camera;
import android.support.v7.app.AppCompatActivity;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Camera camera;
private boolean isOpen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
openCamera();
}
@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
private void openCamera() {
isOpen = false;
try {
releaseCamera();
camera = Camera.open();
isOpen = (camera != null);
} catch (Exception e) {
Log.e(TAG, "openCamera exception " + e.getMessage());
}
}
private void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
}