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 9223599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:06:44+00:00 2026-06-18T04:06:44+00:00

I am using camera in my application,I have no done more work on camera

  • 0

I am using camera in my application,I have no done more work on camera my requirement is only taken picture and with front and back camera and flash light.Camera will be open inside a customview for that I am using this code:-

public class CaptureDealImage extends Activity implements OnClickListener,
    CameraCallback {
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;
private Button objbtncapture, objbtnback, objbtngalary, objbtnretake,
        objbtnuse;
private Button objbtnflashlight, objbbtnfrontcam;// ,flashButton_p,flashButton_l,cameraRotate_p,cameraRotate_l;
private boolean recording;
private TextView show_p, show_l;
int nCurrentOrientation;
private WakeLock wakeLock;
private int count, mode;
private boolean backendCamera = true;
private FrameLayout mymiddlelayout;
private String sdcardpath;
private boolean flashlight = false;
private boolean cameramode = false;
private boolean isfrontcamera = false;
private RelativeLayout objrelativeLayoutretake, objrelativeLayout3;
private byte[] data;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dealpick);
    PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = mgr
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
    // Get Camera for preview
    objbtnflashlight = (Button) findViewById(R.id.btnflashlight);
    objbbtnfrontcam = (Button) findViewById(R.id.bbtnfrontcam);
    objbtncapture = (Button) findViewById(R.id.btncapture);
    objbtngalary = (Button) findViewById(R.id.btngalary);
    objbtnback = (Button) findViewById(R.id.btnback);
    objbtnretake = (Button) findViewById(R.id.btnretake);
    objbtnuse = (Button) findViewById(R.id.btnuse);
    objrelativeLayoutretake = (RelativeLayout) findViewById(R.id.relativeLayoutretake);
    objrelativeLayout3 = (RelativeLayout) findViewById(R.id.relativeLayout3);
    objbtnback.setOnClickListener(this);
    objbtncapture.setOnClickListener(this);
    objbtngalary.setOnClickListener(this);
    objbtnflashlight.setOnClickListener(this);
    objbbtnfrontcam.setOnClickListener(this);
    objbtnretake.setOnClickListener(this);
    objbtnuse.setOnClickListener(this);
}

private Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
        Display getOrient = getWindowManager().getDefaultDisplay();
        if (getOrient.getHeight() > getOrient.getWidth()) {
            c.setDisplayOrientation(90);
        }
    } catch (Exception e) {
    }
    return c; // returns null if camera is unavailable
}

@Override
protected void onPause() {
    super.onPause();
    releaseCamera();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    myCamera = getCameraInstance();
    if (myCamera == null) {
        Toast.makeText(CaptureDealImage.this, "Fail to get Camera",
                Toast.LENGTH_LONG).show();
    }
    myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
    mymiddlelayout = (FrameLayout) findViewById(R.id.middlelayout);
    mymiddlelayout.removeAllViews();
    mymiddlelayout.addView(myCameraSurfaceView);
    myCameraSurfaceView.setCallback(this);
}

private void releaseCamera() {
    /*
     * if (myCamera != null) { myCamera.release(); // release the camera for
     * other applications myCamera = null; }
     */
    Camera camera = this.myCameraSurfaceView.getCamera();
    if (camera != null) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    wakeLock.release();
    /*
     * ReleaseRootBitmap mReleaseRootBitmap=new ReleaseRootBitmap();
     * LinearLayout
     * mLinearLayout=(LinearLayout)findViewById(R.id.record_video_parent);
     * mReleaseRootBitmap.unbindDrawables(mLinearLayout);
     */
}

public class MyCameraSurfaceView extends SurfaceView implements
        SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera mCamera;
    private CameraCallback callback = null;
    private boolean isStarted = true;

    public MyCameraSurfaceView(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        initialize(context);
    }

    public MyCameraSurfaceView(Context context) {
        super(context);
        initialize(context);
    }

    public void setCallback(CameraCallback callback) {
        this.callback = callback;
    }

    public void startPreview() {
        mCamera.startPreview();
    }

    public void initialize(Context mcontext) {
        // 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);
    }

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

        if (holder.getSurface() == null) {
            // preview surface does not exist
            return;
        }
        // stop preview before making changes
        try {
            if (null != mCamera) {
                mCamera.stopPreview();
            }
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            if (null != mCamera) {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            }

            /*
             * if (null != camera) { camera.startPreview();
             */
        } catch (Exception e) {
            Log.d("check",
                    "Error starting camera preview: " + e.getMessage());
        }

    }

    public Camera getCamera() {
        return this.mCamera;
    }

    public void startTakePicture() {
        mCamera.autoFocus(new AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                takePicture();
            }
        });
    }

    public void takePicture() {
        mCamera.takePicture(new ShutterCallback() {
            @Override
            public void onShutter() {
                if (null != callback)
                    callback.onShutter();
            }
        }, new PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                if (null != callback)
                    callback.onRawPictureTaken(data, camera);
            }
        }, new PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                if (null != callback)
                    callback.onJpegPictureTaken(data, camera);
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // mCamera.startPreview();
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (Throwable ignored) {
            }
            mCamera.setPreviewDisplay(holder);
            mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                    if (null != callback)
                        callback.onPreviewFrame(data, camera);
                }
            });
        } catch (IOException e) {
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mCamera != null) {
            // camera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    Display display = getWindowManager().getDefaultDisplay();
    if (display.getHeight() > display.getWidth()) {
        myCamera.setDisplayOrientation(90);
        show_l.setVisibility(View.GONE);
        show_p.setVisibility(View.VISIBLE);
    } else {
        myCamera.setDisplayOrientation(0);
        show_l.setVisibility(View.VISIBLE);
        show_p.setVisibility(View.GONE);
    }
}

private Handler checkHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if (recording && count <= 15) {
            if (mode == 1) {
                show_l.setText(count + "/15");
            } else {
                show_p.setText(count + "/15");
            }
        }
    }
};

@Override
public void onClick(View v) {
    if (v.equals(objbtncapture)) {
        myCameraSurfaceView.startTakePicture();
    }
    if (v.equals(objbtngalary)) {
        // releaseCamera();
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        final int ACTIVITY_SELECT_IMAGE = 100;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
    }

    if (v.equals(objbtnflashlight)) {

        if (!isfrontcamera) {
            if (!flashlight) {
                flashlight = true;
                Parameters params = myCamera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                myCamera.setParameters(params);
            } else {
                flashlight = false;
                Parameters params = myCamera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_OFF);
                myCamera.setParameters(params);
            }
        }
    }

    if (v.equals(objbbtnfrontcam)) {

        if (!cameramode) {
            cameramode = true;
            switchToCamera(cameramode);
        } else {
            cameramode = false;
            switchToCamera(cameramode);
        }
    }

    if (v.equals(objbtnback)) {
        finish();
    }
    if (v.equals(objbtnretake)) {
        objrelativeLayoutretake.setVisibility(View.GONE);
        objrelativeLayout3.setVisibility(View.VISIBLE);
        myCameraSurfaceView.startPreview();
    }
    if (v.equals(objbtnuse)) {
        try {
            PhotoComplete(data);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

private void switchToCamera(boolean frontcamera) {
    releaseCamera();
    if (frontcamera) {
        isfrontcamera = true;
        myCamera = getFrontCameraId();
    } else {
        isfrontcamera = false;
        myCamera = getCameraInstance();
    }

    if (myCamera == null) {
        Toast.makeText(CaptureDealImage.this, "fail to get front camera",
                Toast.LENGTH_SHORT).show();
        myCamera = getCameraInstance();
    }
    myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
    mymiddlelayout = (FrameLayout) findViewById(R.id.middlelayout);
    mymiddlelayout.removeAllViews();
    mymiddlelayout.addView(myCameraSurfaceView);
}

Camera getFrontCameraId() {
    int cameraCount = 0;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    Camera camera = null;
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                camera = Camera.open(camIdx);
                Display getOrient = getWindowManager().getDefaultDisplay();
                if (getOrient.getHeight() > getOrient.getWidth()) {
                    camera.setDisplayOrientation(90);
                }
                // myCamera.unlock();
                // mediaRecorder.setCamera(myCamera);
                // myCamera.setParameters(myCamera.getParameters());
            } catch (RuntimeException e) {
                Log.e("",
                        "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }
    return camera;
    // No front-facing camera found
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

}

@Override
public void onShutter() {
    // TODO Auto-generated method stub

}

@Override
public void onRawPictureTaken(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

}

@Override
public void onJpegPictureTaken(byte[] data, Camera camera) {
    try {
        this.data = data;
        objrelativeLayoutretake.setVisibility(View.VISIBLE);
        objrelativeLayout3.setVisibility(View.GONE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void PhotoComplete(byte[] data) throws FileNotFoundException,
        IOException {
    try {
        sdcardpath = String.format(getResources().getString(R.string.path),
                System.currentTimeMillis());
        FileOutputStream outStream = new FileOutputStream(sdcardpath);
        outStream.write(data);
        outStream.close();
        Bundle objbundle = new Bundle();
        Intent objintent = new Intent(CaptureDealImage.this,
                com.flashdeal.mycamera.SetDealImageCategory.class);
        objbundle.putString("from", "camera");
        objbundle.putString("imagepath", sdcardpath);
        Log.e("check===path", sdcardpath);
        objintent.putExtras(objbundle);
        startActivity(objintent);
        finish();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public String onGetVideoFilename() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == RESULT_OK) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        Bundle objbundle = new Bundle();
        Intent objintent = new Intent(CaptureDealImage.this,
                com.flashdeal.mycamera.SetDealImageCategory.class);
        objbundle.putString("from", "camera");
        objbundle.putString("imagepath", filePath);
        objintent.putExtras(objbundle);
        startActivity(objintent);
        finish();
    }
}

}

But this not work and when I click front camera ,again back camera and again on capture button camera become freez.Please anyone guide me or give imp link for my requirement.

  • 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-18T04:06:45+00:00Added an answer on June 18, 2026 at 4:06 am

    Refer this links ::

    They are facing the same issue that you have.Have a look at once .

    Link 1

    Link 2

    Hope this helps 🙂

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

Sidebar

Related Questions

I'm doing a camera application that will automatically geo-tag each picture taken. Using LocationManager.requestLocationUpdates()
i have problem using camera in my Android application i have a form for
We have created an application that records web camera streams using Xuggler, but video
I have an android application which allows the user to take a picture using
I am developing an application where I have to take picture without using Media
I have a flash application that connects to a users webcam using public static
I have created one sample application.For capture image using camera.It is working fine.After capture
I have a HTML/Javascript web application which integrates with a device's camera using PhoneGap.
I have developed simple camera application for Android mobile using flex 4. The problem
I'm working on a security camera application using the webcam that takes a photo

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.