Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:36:32+00:00 2026-06-15T08:36:32+00:00

I have modified the CameraTestActivity so when it scanned the QR code then it

  • 0

I have modified the “CameraTestActivity” so when it scanned the QR code then it will go to next activity.. But every time when I pressed back button to go back “CameraTestActivity”. The app will crash.

I suspect is because onPuase will releaseCamera() so when it come back it cant find the object. If I not releaseCamera then the camera app will locked.

So anyone can teach me how to design the activity flow?

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);
        }
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:36:33+00:00Added an answer on June 15, 2026 at 8:36 am

    If you release the camera inside the onPause() you have to connect to the camera again in the onResume().

    So:

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

    And then you can remove this call from the onCreate

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have modified some open source code but i haven't figured how to change
I have modified the Apple sample code TableSearch, but there is one thing I
I have modified my wpf button but the problem is when i hit the
I have modified a short piece of pyqt code to produce real-time rendering of
Recently I have modified my code to While taking input form STDIN, I moved
I have built ICS source using make -j4, then I have modified the Music
I have a dataset that I have modified into an xml document and then
I have modified the code in this question ,according to the answers there, in
I have modified a file, named index.html then I run git checkout . which
I'm playing around with javafx and I have modified the code of the MediaPleyer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.