1

「CameraTestActivity」を変更したので、QRコードをスキャンすると、次のアクティビティに進みます。ただし、戻るボタンを押して「CameraTestActivity」に戻るたびに。アプリがクラッシュします。

onPuaseがCamera()を解放するため、戻ってきたときにオブジェクトを見つけることができないためだと思います。カメラをリリースしないと、カメラアプリがロックされます。

では、誰でもアクティビティフローの設計方法を教えてもらえますか?

public class CameraTestActivity extends Activity

private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
private boolean barcodeScanned = false;
private boolean previewing = true;

TextView scanText;
Button scanButton;
ImageScanner scanner;
Context tx;
static Intent anotherActivityIntent;

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


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    autoFocusHandler = new Handler();
    mCamera = getCameraInstance();

    tx = this.getApplicationContext();
    /* Instance barcode scanner */
    scanner = new ImageScanner();
    scanner.setConfig(0, Config.X_DENSITY, 3);
    scanner.setConfig(0, Config.Y_DENSITY, 3);

    mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
    FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
    preview.addView(mPreview);

    scanText = (TextView)findViewById(R.id.scanText);

    scanButton = (Button)findViewById(R.id.ScanButton);

    scanButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (barcodeScanned) {
                    barcodeScanned = false;
                    scanText.setText("Scanning...");
                    mCamera.setPreviewCallback(previewCb);
                    mCamera.startPreview();
                    previewing = true;
                    mCamera.autoFocus(autoFocusCB);
                }
            }
        });
}

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

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

public void onResume() {
    super.onResume();       
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e){
    }
    return c;
}

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

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

PreviewCallback previewCb = new PreviewCallback() {
        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);
            String fileName = "";
            if (result != 0) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();

                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) {
                    scanText.setText("barcode result " + sym.getData());
                    fileName += sym.getData();
                    barcodeScanned = true;
                }

                anotherActivityIntent = new Intent(tx, TakePhoto.class);
                anotherActivityIntent.putExtra("fileName",fileName +".jpg");                    

                startActivity(anotherActivityIntent);
            }
        }
    };

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

1 に答える 1

0

内部のカメラを解放した場合はonPause()、でカメラに再度接続する必要がありますonResume()

それで:

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

そして、この呼び出しをから削除できますonCreate

于 2012-11-29T08:33:02.360 に答える