使用/不使用SurfaceView(android)

时间:2016-01-10 16:54:41

标签: java android android-service

我正在开发Android应用程序(我在大学的学期工作) - 所以我最终堆积 - 我在编程方面非常糟糕(刚开始)。我知道,这个问题已经解决了Taking picture from camera without previewBackground Service to take picture with Camera。但它都没有帮助我(是的,我是如此虚伪)。所以,目前我只有这个Main类:

public class MainActivity extends Activity {

public static SurfaceView surfaceView;
public static SurfaceHolder surfaceHolder;
public static Camera camera;
final String LOG_TAG = "myLogs";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.d(LOG_TAG, "onCreateMain");
    setContentView(R.layout.main);

    surfaceView = (SurfaceView) findViewById(R.id.surface);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                camera.setPreviewDisplay(holder);
                camera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

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

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    });
}

public void Start(View view){
    startService(new Intent(this, MyService.class));
    Log.d(LOG_TAG, "onStartMain");
    finish();
}

public void Stop(View view){
    stopService(new Intent(this, MyService.class));}
}

这是服务类:

public class MyService extends Service {

private Camera camera = MainActivity.camera;
private SurfaceView surfaceView = MainActivity.surfaceView;
private SurfaceHolder surfaceHolder = MainActivity.surfaceHolder;
File photoFile;
final String LOG_TAG = "myLogs";

@Override
public void onCreate(){
    super.onCreate();
    Log.d(LOG_TAG, "onCreate");
    File pictures = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    photoFile = new File(pictures, "myphoto.jpg");
}

@Override
public int onStartCommand(Intent intent,int flags, int startId){
    Log.d(LOG_TAG, "onStartCommand");
    camera = Camera.open();
    takePicture();
    return super.onStartCommand(intent, flags, startId);
}

private void takePicture() {
    Log.d(LOG_TAG, "onTakePicture");
    camera.takePicture(null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            try {
                FileOutputStream fos = new FileOutputStream(photoFile);
                fos.write(data);
                fos.close();
                Log.d(LOG_TAG, "PictureTaken");
            } catch (Exception e) {
                Log.d(LOG_TAG,"Exception");
                e.printStackTrace();
            }
        }
    });
}

@Override
public void onDestroy(){
    Log.d(LOG_TAG, "onDestroy");
    releaseCamera();
    super.onDestroy();
}

private void releaseCamera() {
    if (camera != null)
        camera.release();
    camera = null;
}


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

程序在日志

之后堆叠
  

Log.d(LOG_TAG,“onTakePicture”);

如果我停止服务 - 它就行了(调用onDestroy())。问题只是它不拍照或不保存。没有日志消息

  

Log.d(LOG_TAG,“PictureTaken”);

那么,有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我无法运行您的代码,但我认为您应该删除finish()中的MainActivity.Start()调用,因为这会强制关闭应用程序。图片不会立即拍摄,您必须等待Service类中的回调电话onPictureTaken()