1

在开发一个非常基本的相机应用程序时,我遇到了一组非常具体的错误。该代码几乎与android网站上的代码相同。

Galaxy SII 是唯一一款在我的 LogCat 控制台中显示如下错误的手机:

06-28 16:28:40.098: E/Surface(1650): Surface::init token -2 identity 18

“身份”后面的数字发生了变化(42、38 等),但错误仍然存​​在。

我认为这可能与以下课程有关,但仍不确定其含义:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback 

Galaxy SII 可能会导致此错误怎么办?我还在 HTC Vivid 和 Pantech Burst 上进行测试。我担心这可能会导致更大的问题。我只是想了解这个错误可能是什么。

这是来源:

package com.basicam.android;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CamActivity extends Activity {

    private SurfaceView preview=null;
    private SurfaceHolder previewHolder=null;
    private Camera camera=null;
    private boolean inPreview=false;
    private boolean cameraConfigured=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("crap","oncreated");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        preview=(SurfaceView)findViewById(R.id.preview);
        previewHolder=preview.getHolder();
        previewHolder.addCallback(surfaceCallback);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void onResume() {
        super.onResume();

        camera=Camera.open();
        startPreview();
    }

    @Override
    public void onPause() {
        if (inPreview) {
            camera.stopPreview();
        }

        camera.release();
        camera=null;
        inPreview=false;

        super.onPause();
    }

    private Camera.Size getBestPreviewSize(int width, int height,
            Camera cam) {
        Camera.Size result=null;
        Log.d("crap", "can haz preview sizes?");
        int i = 0;
        for (Camera.Size size : cam.getParameters().getSupportedPictureSizes()) {

            Log.d("sizew", ""+size.width);
            if (size.width<=width && size.height<=height) {
                Log.d("sizew", ""+size.width);
                Log.d("sizeh", ""+size.height);
                if (result==null) {
                    result=size;
                }
                else {
                    int resultArea=result.width*result.height;
                    int newArea=size.width*size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }

        return(result);
    }

    private void initPreview(int width, int height) {
        if (camera!=null && previewHolder.getSurface()!=null) {
            try {
                camera.setPreviewDisplay(previewHolder);
            }
            catch (Throwable t) {
                Log.e("PreviewDemo-surfaceCallback",
                        "Exception in setPreviewDisplay()", t);
            }

            if (!cameraConfigured) {
                Camera.Parameters parameters=camera.getParameters();
                Camera.Size size=getBestPreviewSize(width, height,
                        camera);

                if (size!=null) {
                    parameters.setPreviewSize(size.width, size.height);
                    camera.setParameters(parameters);
                    cameraConfigured=true;
                }
            }
        }
    }

    private void startPreview() {
        if (cameraConfigured && camera!=null) {
            camera.startPreview();
            inPreview=true;
        }
    }

    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
            // no-op -- wait until surfaceChanged()
        }

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

            initPreview(width, height);
            startPreview();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // no-op
        }
    };

}
4

0 回答 0