1. Are you ready for the Galaxy S20? Here is everything we know so far!

Back camera and flash is not working.

Discussion in 'Android Development' started by nagamothu, Jun 18, 2019.

  1. nagamothu

    nagamothu Newbie
    Thread Starter

    Hi can any one help me with camera.
    Am trying to create custom camera with three buttons capture, switch camera , and flash .
    when i capture image the image should display in Display.class and save to phone.
    but when i use back camera blank page is diaplaying and when i use front camera image is displaying in display activity. Please check my code and tell me what is the problem. Logcat is not showing any error.

    Custom_camera_activity.java
    Code (Text):
    1.  
    2.   @Override
    3.     protected void onCreate(final Bundle savedInstanceState) {
    4.         super.onCreate( savedInstanceState );
    5.         setContentView( R.layout.activity_custom__camera );
    6.  
    7.         //Flash
    8.         hasFlash = getApplicationContext().getPackageManager()
    9.                 .hasSystemFeature( PackageManager.FEATURE_CAMERA_FLASH);
    10.         if (!hasFlash) {
    11.             // device doesn't support flash
    12.             // Show alert message and close the application
    13.             AlertDialog alert = new AlertDialog.Builder(Custom_CameraActivity.this)
    14.                     .create();
    15.             alert.setTitle("Error");
    16.             alert.setMessage("Sorry, your device doesn't support flash light!");
    17.             alert.setButton("OK", new DialogInterface.OnClickListener() {
    18.                 public void onClick(DialogInterface dialog, int which) {
    19.                     // closing the application
    20.                     finish();
    21.                 }
    22.             });
    23.             alert.show();
    24.             return;
    25.         }
    26.  
    27.  
    28.         getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
    29.         context = this;
    30.  
    31.         mCamera = Camera.open();
    32.         mCamera.setDisplayOrientation( 90 );
    33.         camerapreview = (LinearLayout) findViewById( R.id.camera_preview );
    34.         mCameraPreview = new CameraPreview( context, mCamera );
    35.         camerapreview.addView( mCameraPreview );
    36.  
    37.         capture = (ImageButton) findViewById( R.id.button_capture );
    38.         capture.setOnClickListener( new View.OnClickListener() {
    39.             @Override
    40.             public void onClick(View v) {
    41.                     mCamera.takePicture( null, null, mPicture );
    42.             }
    43.         } );
    44.  
    45.         switchCamera = (ImageButton) findViewById(R.id.btnSwitch);
    46.         switchCamera.setOnClickListener(new View.OnClickListener() {
    47.             @Override
    48.             public void onClick(View v) {
    49.                 //get the number of cameras
    50.                 int camerasNumber = Camera.getNumberOfCameras();
    51.                 if (camerasNumber > 1) {
    52.                     //release the old camera instance
    53.                     //switch camera, from the front and the back and vice versa
    54.  
    55.                     releaseCamera();
    56.                     chooseCamera();
    57.                 } else {
    58.  
    59.  
    60.                 }
    61.             }
    62.         });
    63.  
    64.         flash= (ImageButton)findViewById( R.id.bulb ) ;
    65.         flash.setOnClickListener( new View.OnClickListener() {
    66.             @Override
    67.             public void onClick(View v) {
    68.                 if(isFlashOn){
    69.                     turnOffFlash();
    70.                 }else{
    71.                     turnOnFlash();
    72.                 }
    73.             }
    74.         } );
    75.  
    76.         mCamera.startPreview();
    77.         //initializeCamera();
    78.     }
    79.  
    80.     private void turnOnFlash() {
    81.         if (isFlashOn) {
    82.             if (mCamera != null || params != null) {
    83.                 return;
    84.             }
    85.             params = mCamera.getParameters();
    86.             params.setFlashMode( Camera.Parameters.FLASH_MODE_OFF);
    87.             mCamera.setParameters(params);
    88.             mCamera.stopPreview();
    89.             isFlashOn = false;
    90.         }
    91.     }
    92.  
    93.     private void turnOffFlash() {
    94.  
    95.        if(!isFlashOn) {
    96.             if (mCamera == null || params == null) {
    97.                 return;
    98.             }
    99.             params = mCamera.getParameters();
    100.             ((Camera.Parameters) params).setFlashMode( Camera.Parameters.FLASH_MODE_TORCH);
    101.            mCamera.setParameters(params);
    102.            mCamera.startPreview();
    103.             isFlashOn = true;
    104.  
    105.         }
    106.  
    107.     }
    108.  
    109.     private void initializeCamera() {
    110.         int cameraId= findBackFacingCamera();
    111.         if(cameraId >= 0){
    112.             mCamera= Camera.open(cameraId);
    113.             mCamera.setDisplayOrientation( 180 );
    114.             mPicture=getPictureCallBack();
    115.             mCameraPreview.refreshCamera( mCamera );
    116.         }
    117.     }
    118.  
    119.  
    120.     private int findFrontFacingCamera() {
    121.         int cameraId = -1;
    122.         // Search for the front facing camera
    123.         int numberOfCameras = Camera.getNumberOfCameras();
    124.  
    125.         for (int i = 0; i < numberOfCameras; i++) {
    126.             Camera.CameraInfo info = new Camera.CameraInfo();
    127.             Camera.getCameraInfo(i, info);
    128.             if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    129.                 cameraId = i;
    130.                 cameraBack = false;
    131.                 break;
    132.             }
    133.         }
    134.         return cameraId;
    135.  
    136.     }
    137.  
    138.     private int findBackFacingCamera() {
    139.         int cameraId = -1;
    140.         //Search for the back facing camera
    141.         //get the number of cameras
    142.         int numberOfCameras = Camera.getNumberOfCameras();
    143.         //for every camera check
    144.         for (int i = 0; i < numberOfCameras; i++) {
    145.             Camera.CameraInfo info = new Camera.CameraInfo();
    146.             Camera.getCameraInfo(i, info);
    147.             if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
    148.                 cameraId = i;
    149.                 cameraBack = true;
    150.                 break;
    151.  
    152.             }
    153.  
    154.         }
    155.         return cameraId;
    156.     }
    157.  
    158.     @Override
    159.     protected void onResume() {
    160.         super.onResume();
    161.         if(mCamera== null){
    162.             mCamera = Camera.open();
    163.             mCamera.setDisplayOrientation( 90 );
    164.             mPicture= getPictureCallBack();
    165.             mCameraPreview.refreshCamera(mCamera);
    166.             Log.d("nu","null");
    167.         }else{
    168.             Log.d("nu","no null");
    169.         }
    170.     }
    171.  
    172.     public void chooseCamera() {
    173.         //if the camera preview is the front
    174.         if (cameraBack) {
    175.             int cameraId = findFrontFacingCamera();
    176.             if (cameraId >= 0) {
    177.                 //open the backFacingCamera
    178.                 //set a picture callback
    179.                 //refresh the preview
    180.  
    181.                 mCamera = Camera.open(cameraId);
    182.                 mCamera.setDisplayOrientation(90);
    183.                 mPicture = getPictureCallBack();
    184.                 mCameraPreview.refreshCamera(mCamera);
    185.             }
    186.         } else {
    187.             int cameraId = findBackFacingCamera();
    188.             if (cameraId >= 0) {
    189.                 //open the backFacingCamera
    190.                 //set a picture callback
    191.                 //refresh the preview
    192.                 mCamera = Camera.open(cameraId);
    193.                 mCamera.setDisplayOrientation(90);
    194.                 mPicture = getPictureCallBack();
    195.                 mCameraPreview.refreshCamera(mCamera);
    196.             }
    197.         }
    198.     }
    199.  
    200.     @Override
    201.     protected void onPause() {
    202.         super.onPause();
    203.         releaseCamera();
    204.     }
    205.  
    206.     private void releaseCamera() {
    207.         if(mCamera != null){
    208.             mCamera.stopPreview();
    209.             mCamera.setPreviewCallback( null );
    210.             mCamera.release();
    211.             mCamera= null;
    212.         }
    213.     }
    214.     private Camera.PictureCallback getPictureCallBack() {
    215.         Camera.PictureCallback picture = new Camera.PictureCallback() {
    216.             @Override
    217.             public void onPictureTaken(byte[] data, Camera camera) {
    218.                 bitmap = BitmapFactory.decodeByteArray( data,0,data.length );
    219.                 Intent intent= new Intent(Custom_CameraActivity.this,Display.class);
    220.                 startActivity( intent );
    221.             }
    222.         };
    223.         return  picture;
    224.     }
    225.  
    226. }
    227.  
    CameraPreview.java
    Code (Text):
    1.  
    2. public class CameraPreview extends SurfaceView implements
    3.         SurfaceHolder.Callback {
    4.     private SurfaceHolder mHolder;
    5.     private Camera mCamera;
    6.  
    7.  
    8.     // Constructor that obtains context and camera
    9.     @SuppressWarnings("deprecation")
    10.     public CameraPreview(Context context, Camera camera) {
    11.         super(context);
    12.         this.mCamera = camera;
    13.         this.mHolder = this.getHolder();
    14.         this.mHolder.addCallback(this);
    15.         this.mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    16.     }
    17.  
    18.     @Override
    19.     public void surfaceCreated(SurfaceHolder holder) {
    20.         try {
    21.             // create the surface and start camera preview
    22.             if (mCamera == null) {
    23.                 mCamera.setPreviewDisplay(holder);
    24.                 mCamera.startPreview();
    25.             }
    26.         } catch (IOException e) {
    27.             Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
    28.         }
    29.     }
    30.  
    31.     public void refreshCamera(Camera camera){
    32.         if(mHolder.getSurface()== null){
    33.             return;
    34.         }
    35.         try{
    36.             mCamera.stopPreview();
    37.         }catch (Exception e){
    38.  
    39.         }
    40.         setCamera(camera);
    41.         try{
    42.             mCamera.setPreviewDisplay( mHolder );
    43.             mCamera.startPreview();
    44.         }catch (Exception e){
    45.             Log.d(VIEW_LOG_TAG,"Error Starting camera preview: " +e.getMessage());
    46.  
    47.         }
    48.     }
    49.  
    50.     private void setCamera(Camera camera) {
    51.         mCamera = camera;
    52.     }
    53.  
    54.     @Override
    55.     public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    56.  
    57.     }
    58.  
    59.     @Override
    60.     public void surfaceChanged(SurfaceHolder Holder, int format,
    61.                                int width, int height) {
    62.         // start preview with new settings
    63.        refreshCamera( mCamera );
    64.  
    65.     }
    66.  
    67.  
    68. }
    69.  
    Display.java
    Code (Text):
    1.  
    2. protected void onCreate(Bundle savedInstanceState) {
    3.     super.onCreate( savedInstanceState );
    4.     setContentView( R.layout.activity_display );
    5.  
    6.     image = findViewById( R.id.imageView );
    7.     scancode=findViewById( R.id.scan );
    8.     scancode.setOnClickListener( this );
    9.  
    10.     image.setImageBitmap( Custom_CameraActivity.bitmap);
    11.     saveImage(Custom_CameraActivity.bitmap);
    12.  
    13.  
    14. private String  saveImage(Bitmap mybitmap) {
    15.     ByteArrayOutputStream bytes = new ByteArrayOutputStream(  );
    16.     mybitmap.compress( Bitmap.CompressFormat.JPEG,90,bytes );
    17.     File file= new File( Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY );
    18.     if(!file.exists() ){
    19.         Log.d("dirrrrrr","" + file.mkdirs());
    20.         file.mkdirs();
    21.     }
    22.     try{
    23.         File f = new File( file, Calendar.getInstance().getTimeInMillis() + ".jpg");
    24.         f.createNewFile();
    25.         FileOutputStream fo = new FileOutputStream( f );
    26.         fo.write( bytes.toByteArray() );
    27.         MediaScannerConnection.scanFile( this,new String[]{f.getPath()} ,
    28.                 new String[]{"image/jpeg"},null);
    29.         fo.close();
    30.         Log.d( "TAG","File Saved:: ---- " + f.getAbsolutePath() );
    31.         return f.getAbsolutePath();
    32.     }catch(IOException e1){
    33.         e1.printStackTrace();
    34.     }
    35.     return "";
    36. }
    37.  
    Please check this code and help me to trace the issue.
     


Loading...

Share This Page

Loading...