使用AndAR Artoolkit和surfaceview显示相机

时间:2015-03-03 23:22:47

标签: android andar

使用以下代码,我可以使用startpreview方法显示相机,但我无法使用andAR库(https://code.google.com/p/andar/)的预览,它会显示黑屏。我不明白是不是因为它过载了。

package edu.dhbw.andar.sample;

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

import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import edu.dhbw.andar.ARToolkit;
import edu.dhbw.andar.AndARActivity;
import edu.dhbw.andar.exceptions.AndARException;

/**
* Example of an application that makes use of the AndAR toolkit.
*
* @author Tobi
*
*/
public class CustomActivity extends AndARActivity implements SurfaceHolder.Callback {

CustomObject someObject;
ARToolkit artoolkit;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera camera;

@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        CustomRenderer renderer = new CustomRenderer();// optional, may be set
                                                                                                        // to null
        super.setNonARRenderer(renderer);// or might be omited
        try {
                // register a object for each marker type
                artoolkit = super.getArtoolkit();
                someObject = new CustomObject("test", "patt.hiro", 80.0,
                                new double[] { 0, 0 });
                artoolkit.registerARObject(someObject);
                someObject = new CustomObject("test", "android.patt", 80.0,
                                new double[] { 0, 0 });
                artoolkit.registerARObject(someObject);
                someObject = new CustomObject("test", "barcode.patt", 80.0,
                                new double[] { 0, 0 });
                artoolkit.registerARObject(someObject);
        } catch (AndARException ex) {
                // handle the exception, that means: show the user what happened
                System.out.println("");
        }
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        // TODO Auto-generated method stub
        startCamera(holder, width, height);

}

@Override
public SurfaceView getSurfaceView() {
        // TODO Auto-generated method stub
        return super.getSurfaceView();
}

@Override
public void startPreview() {
        // TODO Auto-generated method stub
        super.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.surfaceDestroyed(holder);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera = Camera.open();
try {
    camera.setPreviewDisplay(holder);
} catch (IOException e) {
}
}

/**
 * Inform the user about exceptions that occurred in background threads.
 * This exception is rather severe and can not be recovered from. TODO
 * Inform the user and shut down the application.
 */
@Override
public void uncaughtException(Thread thread, Throwable ex) {
        Log.e("AndAR EXCEPTION", ex.getMessage());
        finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.main, menu);
        return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
        // case R.id.settings:
        // break;
        default:
                break;
        }
        return super.onOptionsItemSelected(item);
}

private void startCamera(SurfaceHolder sh, int width, int height) {
        Camera.Parameters parameters = camera.getParameters();  
           List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();  
           Camera.Size cs = sizes.get(0);  
           parameters.setPreviewSize(cs.width, cs.height);  
           camera.setParameters(parameters);

    try {
        camera.setPreviewDisplay(sh);
    } catch (Exception e) {
    }

    camera.startPreview();
}

private void stopCamera() {
        //camera.removeCallback(this);

        camera.stopPreview();
    camera.release();
}

}

1 个答案:

答案 0 :(得分:1)

它适用于我的电脑和手机。我认为问题是权限。您应该向清单文件添加AndAr所需的权限。

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />