2

我从这里看到了这个。我只是想试试它是否有效。链接:http ://www.javacodegeeks.com/2011/03/android-http-camera-live-preview.html

这是第一个名为 MyCamAppActivity.java 的类

    package com.javacodegeeks.android.camera;
import android.app.Activity;
import android.os.Bundle;
import android.view.SurfaceView;    
    import android.view.Window;  
public class MyCamAppActivity extends Activity {         
    private SurfaceView cameraPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        cameraPreview = new CameraPreview(this);

        setContentView(cameraPreview);
    }           
     }

这是第二类,称为 CameraPreview

    package com.javacodegeeks.android.camera;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder holder;
    private Camera camera;

    public CameraPreview(Context context) {
        super(context);
        holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder2, int format, int w, int h) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(w, h);
        camera.setParameters(parameters);
        camera.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder1) {
        try {
            camera = Camera.open();
            camera.setPreviewDisplay(holder1);
        }
        catch (Exception e) {
            Log.i("Exception surfaceCreated()", "e=" + e);
            camera.release();
            camera = null;
          }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }

}

据我所知,当您在 android 编程中使用 surfaceview 时,它会自动在您的屏幕上生成图片。但就我而言,它只是纯黑色图片。顺便说一句,站点中的完整包中没有任何 .xml 内容。所以,我不知道我是否还需要修复 xml 中的某些内容。希望有人或你们中的任何人能尽快帮助我。

4

0 回答 0