相机视图不会在按钮的单击侦听器上显示

时间:2015-05-22 10:28:11

标签: android android-intent android-camera

我正在尝试将相机放在imageview的setonclick监听器上,但是当我运行应用程序并点击imageview时,我的应用程序崩溃并显示..

java.lang.runtimeexception method called after release()

我不知道什么是问题以及为什么我无法在任何时候进行活动,而且我的应用程序崩溃了,提前感谢,

MainMenu.java

 rl_scan.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, ScanDispatch.class);
            startActivity(intent);
            finish();

        }
    });

ScanDispatch.java

 public class ScanDispatch extends Activity {


private TextView current_office_name;

// scanning
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
ImageScanner scanner;
boolean barcodeScanned = false;
boolean previewing = true;
// private DrawerLayout mDrawerLayout;
ImageView img_navigation_link;

private FrameLayout preview;

public ScanDispatch() {
}



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_scan_dispatch);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    consts.pref = getSharedPreferences("pref", MODE_PRIVATE);
    consts.editor = consts.pref.edit();

    current_office_name = (TextView) findViewById(R.id.current_office_name);
    current_office_name.setText(consts.pref.getString("office_name", ""));
    // btn_scan = (ImageButton) findViewById(R.id.btn_scan);
    img_navigation_link = (ImageView) findViewById(R.id.img_navigation_link);



    // scanner


    autoFocusHandler = new Handler();
    mCamera = getCameraInstance();
    scanner = new ImageScanner();
    scanner.setConfig(0, Config.X_DENSITY, 3);
    scanner.setConfig(0, Config.Y_DENSITY, 3);

    mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);

    try {

        if (barcodeScanned) {
            barcodeScanned = false;
            // scanText.setText("Scanning...");
            mCamera.setPreviewCallback(previewCb);
            mCamera.startPreview();
            previewing = true;
            mCamera.autoFocus(autoFocusCB);
        }

    } catch (ActivityNotFoundException anfe) {
        anfe.printStackTrace();

    }

    preview = (FrameLayout) findViewById(R.id.relativeLayout1);

     preview.addView(mPreview);

     img_navigation_link.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            //  mCamera.release();
                //preview.removeView(mPreview);
                /*mCamera.setPreviewCallback(null);
                mCamera.release();
                preview.removeView(mPreview);*/
                Intent intent = new Intent(ScanDispatch.this, MainMenu.class);
                startActivity(intent);


            }
        });



}



public void onPause() {
    super.onPause();
    releaseCamera();
}

public void onResume() {
    super.onResume();
    if (mCamera == null) {
        mCamera = getCameraInstance();
        }

        if (mPreview == null) {

            mCamera.setPreviewCallback(previewCb);
            mCamera.startPreview();
            previewing = true;
            mCamera.autoFocus(autoFocusCB);

        mPreview = new CameraPreview(getApplicationContext(), mCamera, previewCb, autoFocusCB);

        preview.addView(mPreview);
        }
}





@SuppressWarnings("deprecation")
public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e) {
    }
    return c;
}

static {
    System.loadLibrary("iconv");
}

private Runnable doAutoFocus = new Runnable() {
    @SuppressWarnings("deprecation")
    public void run() {
        if (previewing)
            mCamera.autoFocus(autoFocusCB);
    }
};

private void releaseCamera() {
    if (mCamera != null) {
        previewing = false;
        mCamera.setPreviewCallback(null);
        mCamera.release();
        mCamera = null;
    }
}

// Mimic continuous auto-focusing
@SuppressWarnings("deprecation")
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
    public void onAutoFocus(boolean success, Camera camera) {
        autoFocusHandler.postDelayed(doAutoFocus, 1000);
    }
};

@SuppressWarnings("deprecation")
PreviewCallback previewCb = new PreviewCallback() {
    private String degd;

    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        Size size = parameters.getPreviewSize();

        Image barcode = new Image(size.width, size.height, "Y800");
        barcode.setData(data);

        int result = scanner.scanImage(barcode);

        if (result != 0) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();

            SymbolSet syms = scanner.getResults();
            for (Symbol sym : syms) {
                // scanText.setText("barcode result " + sym.getData());
                Log.d("ooo", "Barcode result:" + sym.getData());
                String contents = sym.getData();
                consts.editor.putString("qrcode", contents);
                consts.editor.commit();

                barcodeScanned = true;

                Intent intent = new Intent(ScanDispatch.this, DispatchTracking.class);
                intent.putExtra("arrow_val", "1");
                startActivity(intent);
                mCamera.release();
                ScanDispatch.this.finish();

                // Fragment fragment = new DispatchTracking();
                //
                // if (fragment != null) {
                // FragmentManager fragmentManager = getFragmentManager();
                // fragmentManager.beginTransaction()
                // .replace(R.id.content_frame, fragment).commit();

                // setTitle("Dispatch Me");
                // mDrawerLayout.closeDrawer(mDrawerList);
                //
                // }

            }
        }
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == Activity.RESULT_OK) {

            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            consts.editor.putString("qrcode", contents);
            consts.editor.commit();

            // Toast.makeText(this, "Content:" + contents + " Format:" +
            // format, Toast.LENGTH_LONG).show();

            Intent intent2 = new Intent(ScanDispatch.this, DispatchTracking.class);
            intent2.putExtra("arrow_val", "1");
            startActivity(intent2);
            ScanDispatch.this.finish();

            // Fragment fragment = new DispatchTracking();
            //
            // if (fragment != null) {
            // FragmentManager fragmentManager = getFragmentManager();
            // fragmentManager.beginTransaction()
            // .replace(R.id.content_frame, fragment).commit();
            //
            // // setTitle("Dispatch Me");
            // // mDrawerLayout.closeDrawer(mDrawerList);
            //
            // }

        }
    }
}

CameraPreview.java

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private PreviewCallback previewCallback;
    private AutoFocusCallback autoFocusCallback;

    public CameraPreview(Context context, Camera camera,
                         PreviewCallback previewCb,
                         AutoFocusCallback autoFocusCb) {
        super(context);
        mCamera = camera;
        previewCallback = previewCb;
        autoFocusCallback = autoFocusCb;

        /* 
         * Set camera to continuous focus if supported, otherwise use
         * software auto-focus. Only works for API level >=9.
         */
        /*
        Camera.Parameters parameters = camera.getParameters();
        for (String f : parameters.getSupportedFocusModes()) {
            if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
                mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                autoFocusCallback = null;
                break;
            }
        }
        */

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);

        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @SuppressWarnings("deprecation")
    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.d("DBG", "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Camera preview released in activity

        /*this.getHolder().removeCallback(this);
        mCamera.stopPreview();
        mCamera.release();*/
        //mCamera.release();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        /*
         * If your preview can change or rotate, take care of those events here.
         * Make sure to stop the preview before resizing or reformatting it.
         */
        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        try {
            // Hard code camera surface rotation 90 degs to match Activity view in portrait
            mCamera.setDisplayOrientation(90);

            mCamera.setPreviewDisplay(mHolder);
            mCamera.setPreviewCallback(previewCallback);
            mCamera.startPreview();
            mCamera.autoFocus(autoFocusCallback);
        } catch (Exception e){
            Log.d("DBG", "Error starting camera preview: " + e.getMessage());
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你必须在camera.release()之前取消设置预览回调。否则可能会在相机释放后调用。所以添加这一行:

mCamera.setPreviewCallback(空);