mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-01-14 08:57:53 -05:00
136 lines
3.8 KiB
Java
136 lines
3.8 KiB
Java
/*
|
|
Copyright 2012 Bruno Carreira - Lucas Farias - Rafael Luna - Vinícius Fonseca.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package com.foregroundcameraplugin;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import android.app.Activity;
|
|
import android.hardware.Camera;
|
|
import android.hardware.Camera.AutoFocusCallback;
|
|
import android.hardware.Camera.PictureCallback;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.provider.MediaStore;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.FrameLayout;
|
|
|
|
/**
|
|
* Camera Activity Class. Configures Android camera to take picture and show it.
|
|
*/
|
|
public class CameraActivity extends Activity {
|
|
|
|
private static final String TAG = "CameraActivity";
|
|
|
|
private Camera mCamera;
|
|
private ForegroundCameraPreview mPreview;
|
|
private boolean pressed = false;
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.foregroundcameraplugin);
|
|
|
|
// Create an instance of Camera
|
|
mCamera = getCameraInstance();
|
|
|
|
// Create a Preview and set it as the content of activity.
|
|
mPreview = new ForegroundCameraPreview(this, mCamera);
|
|
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
|
|
preview.addView(mPreview);
|
|
|
|
// Add a listener to the Capture button
|
|
Button captureButton = (Button) findViewById(R.id.button_capture);
|
|
captureButton.setOnClickListener(new View.OnClickListener() {
|
|
public void onClick(View v) {
|
|
|
|
if (pressed)
|
|
return;
|
|
|
|
// Set pressed = true to prevent freezing.
|
|
// Issue 1 at
|
|
// http://code.google.com/p/foreground-camera-plugin/issues/detail?id=1
|
|
pressed = true;
|
|
|
|
// get an image from the camera
|
|
mCamera.autoFocus(new AutoFocusCallback() {
|
|
|
|
public void onAutoFocus(boolean success, Camera camera) {
|
|
mCamera.takePicture(null, null, mPicture);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
Button cancelButton = (Button) findViewById(R.id.button_cancel);
|
|
cancelButton.setOnClickListener(new View.OnClickListener() {
|
|
public void onClick(View v) {
|
|
pressed = false;
|
|
setResult(RESULT_CANCELED);
|
|
finish();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
if (mCamera != null) {
|
|
mCamera.release(); // release the camera for other applications
|
|
mCamera = null;
|
|
}
|
|
super.onPause();
|
|
}
|
|
|
|
/** A safe way to get an instance of the Camera object. */
|
|
public static Camera getCameraInstance() {
|
|
Camera c = null;
|
|
try {
|
|
c = Camera.open(); // attempt to get a Camera instance
|
|
} catch (Exception e) {
|
|
// Camera is not available (in use or does not exist)
|
|
}
|
|
return c; // returns null if camera is unavailable
|
|
}
|
|
|
|
private PictureCallback mPicture = new PictureCallback() {
|
|
|
|
public void onPictureTaken(byte[] data, Camera camera) {
|
|
|
|
Uri fileUri = (Uri) getIntent().getExtras().get(
|
|
MediaStore.EXTRA_OUTPUT);
|
|
|
|
File pictureFile = new File(fileUri.getPath());
|
|
|
|
try {
|
|
FileOutputStream fos = new FileOutputStream(pictureFile);
|
|
fos.write(data);
|
|
fos.close();
|
|
} catch (FileNotFoundException e) {
|
|
Log.d(TAG, "File not found: " + e.getMessage());
|
|
} catch (IOException e) {
|
|
Log.d(TAG, "Error accessing file: " + e.getMessage());
|
|
}
|
|
setResult(RESULT_OK);
|
|
pressed = false;
|
|
finish();
|
|
}
|
|
};
|
|
} |