Android相机活动需要一些时间来渲染

时间:2014-07-16 05:05:59

标签: java android android-fragments android-camera android-camera-intent

我有一个简单的app.On按钮点击主要活动,我打电话给另一个活动,它只显示前面Camera的预览。我发现第二个活动需要大约4秒才能启动。我认为是因为在第二个活动中渲染相机预览元素所花费的时间。这太长了,我正在寻找一种方法来减少第二个活动的渲染时间。这是我的代码:

Main.java

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnLaunchCamera = (Button) findViewById(R.id.launchCamera);
        btnLaunchCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Main.this, Cam.class));
            }
        });
    }
}

Cam.java

public class Cam extends Activity {

    private static int cameraId = 0;
    private Camera camera;

    //Adding for camera preview
    public static FrameLayout preview;
    public static CameraPreview mPreview;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("Cam","Inside onCreate");
        setContentView(R.layout.cam);
        context = getApplicationContext();

        // Do we have a camera?
        try {
            if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
            } else {
                cameraId = AppFunctions.findFrontFacingCamera();
                if (cameraId < 0) {
                    Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
                } else {

                    try {
                        camera = Camera.open(cameraId);
                        Log.d("Cam","camera id" + cameraId);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        mPreview = new CameraPreview(this, camera);
                        preview = (FrameLayout) findViewById(R.id.camera_preview);
                        preview.addView(mPreview);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e3) {
            e3.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.e("Cam","Inside onPause");
        try {
            if (camera != null) {
                camera.release();
                camera = null;
                preview.removeView(mPreview);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("Cam","Inside onResume");
        try {
            if(camera ==null) {
                Log.d("Cam","in resume,camera is  null");
                try {
                    camera = android.hardware.Camera.open(cameraId);                
                    mPreview = new CameraPreview(this, camera);
                    preview = (FrameLayout) findViewById(R.id.camera_preview);
                    preview.addView(mPreview);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Cam","Inside onDestroy");
        if (camera != null) {
            try {
                camera.release();
                camera = null;
                preview.removeView(mPreview);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

cam.xml

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="453dp"
        android:layout_height="400dp"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="30dp"
        android:padding="5dp" >
    </FrameLayout>

CameraPreview.java

    /** A basic Camera preview class */
    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        private SurfaceHolder mHolder;
        private Camera mCamera;
        final static String TAG = "CameraPreview";

        public CameraPreview(Context context, Camera camera) {
            super(context);

            try {
                mCamera = camera;
                mHolder = getHolder();
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.setDisplayOrientation(0);
                mCamera.startPreview();
            } catch (Exception e) {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG,"Inside SurfaceDestroyed");
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            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
            }

            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
            } catch (Exception e){
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }
    }

请协助我如何减少第二次活动的渲染时间。非常感谢。非常感谢!

0 个答案:

没有答案