即使在将工作卸载到新线程后也跳过帧

时间:2016-05-09 18:10:15

标签: android multithreading runnable

我正在制作一款应用,可在用户解锁手机后自动拍照。我有一个运行的服务,支持广播接收器,响应成功的电话输入。我在实际拍照和保存到外部存储时遇到问题。它正在跳帧,并在拍摄完照片之后存储图像分钟,或者根本不存储。我已经将拍照卸载到AsyncTask以及通过Runnable启动一个新的Thread。没什么,请帮忙。

public class MasterService extends Service {

private BroadcastReceiver receiver;

@Override
public void onCreate() {

    receiver = new BroadcastReceiver() {

        private Camera camera;
        private SurfaceView preview;
        private SurfaceHolder holder;

        @Override
        public void onReceive(Context context, Intent intent) {




            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {

                //code for other versions
            }
            else {
                preview = new SurfaceView(context);
                holder = preview.getHolder();
                holder.addCallback(surfaceCallback);

                WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                WindowManager.LayoutParams lParams = new WindowManager.LayoutParams(1000, 1000, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.UNKNOWN);

                wm.addView(preview, lParams);
            }
        }

        SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

            public void surfaceCreated(SurfaceHolder holder) {
                Camera.CameraInfo info=new Camera.CameraInfo();

                for (int i=0; i < Camera.getNumberOfCameras(); i++) {
                    Camera.getCameraInfo(i, info);

                    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                        camera = Camera.open(i);
                    }
                }

            }

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

                try
                {
                    camera.setPreviewDisplay(holder);
                }
                catch (Throwable t)
                {

                }

                Camera.Parameters cParams = camera.getParameters();

                cParams.setJpegQuality(100);
                camera.setParameters(cParams);

                camera.startPreview();

                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {

                        camera.takePicture(null, null, new Camera.PictureCallback() {

                            @Override
                            public void onPictureTaken(byte[] data, Camera camera) {
                                File photo = new File(Environment.getExternalStorageDirectory(), "photo.jpg");

                                if (photo.exists()) {
                                    photo.delete();
                                }

                                try
                                {
                                    FileOutputStream fos=new FileOutputStream(photo.getPath());

                                    fos.write(data);
                                    fos.close();
                                }
                                catch (java.io.IOException e)
                                {

                                }
                            }
                        });
                    }
                });

                thread.start();
            }

            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        };


    };
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    registerReceiver(receiver, new IntentFilter(Intent.ACTION_USER_PRESENT));

    return START_STICKY;
}

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

@Override
public void onDestroy() {
    unregisterReceiver(receiver);
}

}

0 个答案:

没有答案