如何在相机预览上绘画? (java安卓)

时间:2019-01-15 16:58:07

标签: java android camera draw

我需要获取相机预览,然后在相机顶部绘制一个正方形。 我设法从相机上获取了图像,但没有画出一个正方形。应用程序崩溃。 帮助我弄清楚如何最好地实现这一点?

我试图画画,但正方形很快消失或根本没有出现。我真的很想了解这一点。

ShowCamera.java

dat$group[which(dat$z == df$y)] <- df$x[which(df$y == dat$z)]
dat$group[which(dat$z %in% df$y)] <- df$x[which(df$y %in% dat$z)]

MainActivity.java

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
Camera camera;
SurfaceHolder holder;

Path path;

volatile boolean running = false;

public ShowCamera(Context context, Camera camera) {
    super(context);
    this.camera = camera;
    holder = getHolder();
    holder.addCallback(this);
}


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

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Camera.Parameters params = camera.getParameters();

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size mSize = null;

    for(Camera.Size size : sizes){
        mSize = size;
    }
    //change orientation
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
        params.set("orientation","portrait");
        camera.setDisplayOrientation(90);
        params.setRotation(90);
    }
    else{
        params.set("orientation","landscape");
        camera.setDisplayOrientation(0);
        params.setRotation(0);
    }

    params.setPictureSize(mSize.width,mSize.height);

    camera.setParameters(params);
    try{
        camera.setPreviewDisplay(holder);
        camera.startPreview();
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
}

0 个答案:

没有答案