我想在android中拍摄连续照片

时间:2015-09-10 06:32:24

标签: android android-intent

在这里,我尝试使用按钮点击和服务在后台拍摄连续照片。我可以连续拍照。我编写代码的方式只有在我点击按钮后才能工作,但是当我试图关闭应用程序时,服务就会自动启动。请帮我解决错误。

服务类:

public class RecorderService extends Service implements     
SurfaceHolder.Callback {
private WindowManager windowManager;
private SurfaceView surfaceView;
private Camera camera = null;

@Override
public void onCreate() {
    showMessage("Entered");
    // Create new SurfaceView, set its size to 1x1, move it to the top left 
corner and set this service as a callback
    windowManager = (WindowManager) 
this.getSystemService(Context.WINDOW_SERVICE);
    surfaceView = new SurfaceView(this);
    WindowManager.LayoutParams layoutParams = new    
WindowManager.LayoutParams(
            1, 1,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT
    );
    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    windowManager.addView(surfaceView, layoutParams);
    surfaceView.getHolder().addCallback(this);
    showMessage("Exited Oncreate");

}

// Method called right after Surface created (initializing and starting 
MediaRecorder)
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        camera = Camera.open();
        showMessage("Opened camera");
        try {
            camera.setPreviewDisplay(surfaceHolder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        camera.startPreview();
        showMessage("Started preview");
        new CountDownTimer(25000, 5000) {
            @Override
            public void onFinish() {
                camera.lock();
                camera.release();
                windowManager.removeView(surfaceView);
                showMessage("Finished preview");
            }
            @Override
            public void onTick(long millisUntilFinished) {
                camera.takePicture(null, null, new Camera.PictureCallback()   
 {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        File pictureFile = getOutputMediaFile();
                        if (pictureFile == null) {
                            showMessage("No picture");
                            return;
                        }
                        try {
                            showMessage("Picture present");
                            FileOutputStream fos = new 
FileOutputStream(pictureFile);
                            fos.write(data);
                            showMessage("writing Done");
                        } catch (FileNotFoundException e) {
                        } catch (IOException e) {
                        }
                        showMessage("Took picture");
                        Toast.makeText(getApplicationContext(), "Picture  
Captured",
                                Toast.LENGTH_SHORT).show();
            }
        });
    }
    }.start();
}
    catch (Exception e) {
    if (camera != null)
        camera.release();
    throw new RuntimeException(e);
    }
}
    private File getOutputMediaFile() {
            File mediaStorageDir = new 

 File(Environment.getExternalStoragePublicDirectory
 (Environment.DIRECTORY_PICTURES),
                    "MyCameraApp");
            showMessage("Picture present");
            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;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");

            return mediaFile;
        }
private static void showMessage(String message) {
    Log.i("Camera", message);
}

// Stop recording and remove SurfaceView
@Override
public void onDestroy() {
    showMessage("Entered OnDestroy");
    camera.lock();
    camera.release();
    windowManager.removeView(surfaceView);
    showMessage("Exited OnDestroy");
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int 
width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

启动Button类:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),   
RecorderService.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent);
        }
    });

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx.xxxx" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".PhotoCapture"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".RecorderService"/>
    <receiver android:name="com.example.xxxx.xxxx">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF">
 </action>
            <action android:name="android.intent.action.SCREEN_ON"></action>
            <action  
  android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
            <action 
  android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
            <action android:name="android.intent.action.ACTION_SHUTDOWN">
  </action>
        </intent-filter>
    </receiver>
</application>



</manifest>

0 个答案:

没有答案