Android相机预览不起作用(HTC MyTouch 3G Slide Android 2.2)

时间:2011-04-13 18:50:30

标签: android android-camera android-2.2-froyo

我看不出我在做什么有什么问题,但是我无法将手机上的相机初始化并开始绘制预览。我创建了一个非常简化的应用程序,它表现出同样的问题,如下所示:

package com.ct;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TestActivity extends Activity implements SurfaceHolder.Callback {
    SurfaceHolder cameraPreviewHolder;
    Camera camera;
    SurfaceView cameraPreview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cameraPreview = (SurfaceView) findViewById(R.id.SurfaceCamera);
        cameraPreviewHolder = cameraPreview.getHolder();
        cameraPreviewHolder.addCallback(this);
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        camera = Camera.open();
        camera.unlock();
        Parameters p = camera.getParameters();

        List<Size> sizes = p.getSupportedPreviewSizes();
        int bestFit=0;
        int difference = Integer.MAX_VALUE;
        for(int i=0; i<sizes.size(); i++){
            Size s = sizes.get(i);
            int dif = (width-s.width)+(height-s.height);
            if(s.width<=width && s.height<=height && dif>0 && dif < difference){
                bestFit = i;
                difference = dif;
            }
        }
        Size s = sizes.get(bestFit);

        p.setPreviewSize(s.width, s.height);
        p.setPictureFormat(PixelFormat.JPEG);
        camera.setParameters(p);


        try {
            camera.setPreviewDisplay(cameraPreviewHolder);
            camera.startPreview();
        } catch (IOException e) {

        }
    }
    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

    }
    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

    }
}

我的logcat显示以下内容:

W/CameraSwitch( 2020): open main camera
W/CameraSwitch( 2020): no file - can't switch camera
E/QualcommCameraHardware( 68): native_access_parm: error (No such file or directory): fd 14, type 1, length 32, status 0
E/mm-camera 7x-vfe( 68): Received VFE start ACK!!! This is a user preview start.
E/CameraService( 68): registerBuffers failed with status -38
W/dalvikvm( 2020): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
W/InputConnectionWrapper.ICC(  176): Timed out waiting on IInputContextCallback

想法?我昨天刚刚更新到Android 2.2,我希望这不会破坏我的相机API ...

1 个答案:

答案 0 :(得分:0)

查看SDK附带的APIDemos / graphics / CameraPreview。

亚历

更新:尝试从PixelFormat更改为ImageFormat。我注意到这是在2.2

中引入的