CameraActivity.java android4.3
package com.example.mycam;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.TargetApi;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.provider.MediaStore.Files.FileColumns;
import android.provider.*;
import android.media.*;
import android.widget.Toast;
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private Camera.Parameters mPara;
private Camera mCamera1;
private CameraPreview mPreview1;
private Camera.Parameters mPara1;
private MediaRecorder mMediaRecorder;
private MediaRecorder mMediaRecorder1;
private static final String TAG = "MyCameraActvity";
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if((android.hardware.Camera.getNumberOfCameras()) == 0){
Log.i(TAG,"mCamera = null");
Toast.makeText(this, "Camera not work!",Toast.LENGTH_LONG).show();
super.onStop();
finish();
}
setContentView(R.layout.activity_camera);
if(mCamera == null){
Log.i(TAG,"mCamera = null");
mCamera = getCameraInstence(0);
}
mPara=mCamera.getParameters();
mPara.setPreviewSize(1920,1080);
mPara.setCallbackFormat(ImageFormat.NV12);
mCamera.setParameters(mPara);
mPreview = new CameraPreview(this,mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
if(mCamera1 == null){
Log.i(TAG,"mCamera1 = null");
mCamera1 = getCameraInstence(1);
}
mPara1=mCamera1.getParameters();
mPara1.setPreviewSize(640,480);
mCamera1.setParameters(mPara1);
mPreview1 = new CameraPreview(this,mCamera1);
FrameLayout preview1 = (FrameLayout) findViewById(R.id.camera_preview1);
preview1.addView(mPreview1);
// mPara=mCamera.getParameters();
Log.i(TAG, "onCreate");
// Log.i(TAG, "getSupportedPreviewFrameRates" + mPara.getSupportedPreviewFrameRates());
// Log.i(TAG, "getSupportedPreviewFrameRates" + mPara.getSupportedPreviewFpsRange());
}
public static Camera getCameraInstence( int num) {
Camera c = null;
try{
c = Camera.open(num);
Log.i(TAG,"Camera test Opening " + num);
}
catch(Exception e){
Log.e(TAG,"Camera test OpenError " + num);
}
// TODO Auto-generated method stub
return c;
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: " /*+ e.getMessage()*/);
return;
}
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());
}
}
};
private boolean prepareVideoRecorder(){
if(mCamera == null){
mCamera = getCameraInstence(0);
}
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));
// mMediaRecorder1.setVideoSize(1280, 720);
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
mMediaRecorder.setAudioChannels(2);
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
private boolean prepareVideoRecorder1(){
if(mCamera1 == null){
mCamera1 = getCameraInstence(1);
}
mMediaRecorder1 = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera1.unlock();
mMediaRecorder1.setCamera(mCamera1);
// Step 2: Set sources
// mMediaRecorder1.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder1.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
// mMediaRecorder1.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mMediaRecorder1.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder1.setVideoSize(1280, 720);
mMediaRecorder1.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mMediaRecorder1.setVideoFrameRate(30);
mMediaRecorder1.setVideoEncodingBitRate(3000000);
//
// Step 4: Set output file
mMediaRecorder1.setOutputFile("/mnt/sdcard/Pictures/video1.mp4");
// mMediaRecorder1.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO1).toString());
// Step 5: Set the preview output
mMediaRecorder1.setPreviewDisplay(mPreview1.getHolder().getSurface());
//mMediaRecorder1.setAudioChannels(2);
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder1.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static final int MEDIA_TYPE_VIDEO1 = 3;
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else if(type == MEDIA_TYPE_VIDEO1) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + "video1.mp4");
} else {
return null;
}
return mediaFile;
}
public void onButtonCloseClick(View view){
Log.i(TAG,"onButtonCloseClick");
mCamera.stopPreview();
mCamera.release();
mCamera1.stopPreview();
mCamera1.release();
super.onStop();
super.onDestroy();
finish();
}
public void onButtonCaptureClick(View view){
Log.i(TAG,"onButtonCaptureClick!");
if(mCamera!=null){
// mCamera.stopPreview();
// mPara.setPreviewSize(1280,720);
mPara.setPictureSize(1280,720);
mCamera.setParameters(mPara);
// mCamera.startPreview();
mCamera.takePicture(null, null, mPicture);
}
}
public void onButton480pClick(View view){
Log.i(TAG,"onButton480pClick!");
if(mCamera!=null){
mCamera.stopPreview();
mPara.setPreviewSize(640,480);
mPara.setPictureSize(640, 480);
// mPara.setCallbackFormat(ImageFormat.NV12);
mCamera.setParameters(mPara);
Log.i(TAG, "getSupportedPreviewFrameRates" + mPara.getSupportedPreviewFrameRates());
mCamera.startPreview();
//mCamera.takePicture(null, null, mPicture);
}
}
public void onButton720pClick(View view){
Log.i(TAG,"onButton720pClick!");
if(mCamera!=null){
mCamera.stopPreview();
mPara.setPreviewSize(1280,720);
mPara.setPictureSize(1280,720);
// mPara.setCallbackFormat(ImageFormat.NV12);
mCamera.setParameters(mPara);
Log.i(TAG, "getSupportedPreviewFrameRates" + mPara.getSupportedPreviewFrameRates());
mCamera.startPreview();
//mCamera.takePicture(null, null, mPicture);
}
}
public void onButton1080pClick(View view){
startRec1();
// Log.i(TAG,"onButton1080pClick!");
// if(mCamera!=null){
// mCamera.stopPreview();
// mPara.setPreviewSize(1920,1080);
// mPara.setPictureSize(1920,1080);
// mCamera.setParameters(mPara);
// Log.i(TAG, "getSupportedPreviewFrameRates" + mPara.getSupportedPreviewFrameRates());
// mCamera.startPreview();
// //mCamera.takePicture(null, null, mPicture);
// }
}
public void onButtonCamera0Click(View view){
Log.i(TAG,"onButtonCamera0Click!");
}
public void onButtonCamera1Click(View view){
Log.i(TAG,"onButtonCamera1Click!");
if(mCamera1 == null){
Log.i(TAG,"mCamera1 = null");
mCamera1 = getCameraInstence(1);
}
mPara1=mCamera1.getParameters();
mPara1.setPreviewSize(640,480);
mCamera1.setParameters(mPara1);
mPreview1 = new CameraPreview(this,mCamera1);
FrameLayout preview1 = (FrameLayout) findViewById(R.id.camera_preview1);
preview1.addView(mPreview1);
}
private boolean isRecording = false;
private boolean isRecording1 = false;
public void onButtonVideoClick(View view){
startRec();
}
public void startRec(){
Log.i(TAG, "onButtonVideoClick!");
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
Log.i(TAG, "Stop Recording!");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
// setCaptureButtonText("Stop");
Log.i(TAG, "Start Recording!");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
public void startRec1(){
Log.i(TAG, "onButtonVideoClick!");
if (isRecording1) {
// stop recording and release camera
mMediaRecorder1.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera1.lock(); // take camera access back from MediaRecorder
Log.i(TAG, "Stop Recording1!");
isRecording1 = false;
} else {
// initialize video camera
if (prepareVideoRecorder1()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder1.start();
// inform the user that recording has started
// setCaptureButtonText("Stop");
Log.i(TAG, "Start Recording!");
isRecording1 = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
protected void onRestart(){
super.onRestart();
Log.i(TAG,"onRestart");
}
protected void onStart(){
super.onStart();
Log.i(TAG,"onStart");
}
protected void onResume(){
super.onResume();
Log.i(TAG, "onResume()");
}
// protected void onPause(){
// super.onPause();
// Log.i(TAG, "onPause");
// }
protected void onStop(){
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
super.onStop();
Log.i(TAG, "onStop");
}
protected void onDestory(){
super.onDestroy();
Log.i(TAG, "onDestory");
}
@Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
if (mMediaRecorder1 != null) {
mMediaRecorder1.reset(); // clear recorder configuration
mMediaRecorder1.release(); // release the recorder object
mMediaRecorder1 = null;
mCamera1.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
if (mCamera1 != null){
mCamera1.release(); // release the camera for other applications
mCamera1 = null;
}
}
}
CameraPreview.java
package com.example.mycam;
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 = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
private Camera.Parameters mPara;
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);
// mPara.setPreviewSize(1280,720);
// mCamera.setParameters(mPara);
// mCamera.startPreview();
// mCamera.stopPreview();
// mPara=mCamera.getParameters();
// mPara.setPreviewSize(1920,1080);
// 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());
}
}
}
myCam_Double\src\com\example\mycam
myCam_Double\res\layout\activity_camera.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.37"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1.37" >
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.37"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_preview1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1.37" >
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<Button
android:id="@+id/button_cam1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonCamera1Click"
android:text="Camera1" />
<Button
android:id="@+id/button_cam0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonCamera0Click"
android:text="Camera0" />
<Button
android:id="@+id/button_1080p"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButton1080pClick"
android:text="1080P" />
<Button
android:id="@+id/button_720p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButton720pClick"
android:text="720P" />
<Button
android:id="@+id/button_480p"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButton480pClick"
android:text="480P" />
<Button
android:id="@+id/button_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButtonVideoClick"
android:text="Video" />
<Button
android:id="@+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonCaptureClick"
android:text="Capture" />
<Button
android:id="@+id/button_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButtonCloseClick"
android:text="Close" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycam"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.size" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mycam.CameraActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>