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

Custom Camera takes low quality pictures

Discussion in 'Android Development' started by S12, Jun 4, 2016.

  1. S12

    S12 Newbie
    Thread Starter

    I have a custom camera and it takes really low quality pictures. I need help to fix this.

    Code (Text):
    1. import android.content.Context;
    2. import android.content.pm.PackageManager;
    3. import android.hardware.Camera;
    4. import android.support.v7.app.AppCompatActivity;
    5. import android.os.Bundle;
    6. import android.view.SurfaceView;
    7. import android.view.WindowManager;
    8. import android.widget.Button;
    9. import android.widget.FrameLayout;
    10. import android.widget.ImageButton;
    11. import android.widget.LinearLayout;
    12. import android.widget.RelativeLayout;
    13. import android.widget.Toast;
    14. import java.io.File;
    15. import java.text.SimpleDateFormat;
    16. import java.util.Date;
    17. import java.io.FileNotFoundException;
    18. import java.io.FileOutputStream;
    19. import java.io.IOException;
    20. import android.hardware.Camera.PictureCallback;
    21. import android.view.View;
    22. import android.view.View.OnClickListener;
    23. public class MainActivity extends AppCompatActivity {
    24.     private Camera mCamera;
    25.     private CameraPreview mPreview;
    26.     private Camera.PictureCallback mPicture;
    27.     private Button switchCamera;
    28.     private Context myContext;
    29.     private FrameLayout cameraPreview;
    30.     private boolean cameraFront = false;
    31.     @Override
    32.     protected void onCreate(Bundle savedInstanceState) {
    33.         super.onCreate(savedInstanceState);
    34.         setContentView(R.layout.activity_main);
    35.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    36.         myContext = this;
    37.         initialize();
    38.     }
    39.     private int findFrontFacingCamera() {
    40.         int cameraId = -1;
    41.         // Search for the front facing camera
    42.         int numberOfCameras = Camera.getNumberOfCameras();
    43.         for (int i = 0; i < numberOfCameras; i++) {
    44.             Camera.CameraInfo info = new Camera.CameraInfo();
    45.             Camera.getCameraInfo(i, info);
    46.             if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    47.                 cameraId = i;
    48.                 cameraFront = true;
    49.                 break;
    50.             }
    51.         }
    52.         return cameraId;
    53.     }
    54.     private int findBackFacingCamera() {
    55.         int cameraId = -1;
    56.         //Search for the back facing camera
    57.         //get the number of cameras
    58.         int numberOfCameras = Camera.getNumberOfCameras();
    59.         //for every camera check
    60.         for (int i = 0; i < numberOfCameras; i++) {
    61.             Camera.CameraInfo info = new Camera.CameraInfo();
    62.             Camera.getCameraInfo(i, info);
    63.             if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
    64.                 cameraId = i;
    65.                 cameraFront = false;
    66.                 break;
    67.             }
    68.         }
    69.         return cameraId;
    70.     }
    71.     public void onResume() {
    72.         super.onResume();
    73.         if (!hasCamera(myContext)) {
    74.             Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
    75.             toast.show();
    76.             finish();
    77.         }
    78.         if (mCamera == null) {
    79.             //if the front facing camera does not exist
    80.             if (findFrontFacingCamera() < 0) {
    81.                 Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
    82.                 switchCamera.setVisibility(View.GONE);
    83.             }
    84.             mCamera = Camera.open(findBackFacingCamera());
    85.             mPicture = getPictureCallback();
    86.             mPreview.refreshCamera(mCamera);
    87.         }
    88.     }
    89.     public void initialize() {
    90.         mPreview = new CameraPreview(myContext, mCamera);
    91.         cameraPreview = (FrameLayout) findViewById(R.id.camera_preview);
    92.         cameraPreview.addView(mPreview);
    93.         ImageButton capture = (ImageButton) findViewById(R.id.button_capture);
    94.         capture.setOnClickListener(captrureListener);
    95.         switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
    96.         switchCamera.setOnClickListener(switchCameraListener);
    97.     }
    98.     OnClickListener switchCameraListener = new OnClickListener() {
    99.         @Override
    100.         public void onClick(View v) {
    101.             //get the number of cameras
    102.             int camerasNumber = Camera.getNumberOfCameras();
    103.             if (camerasNumber > 1) {
    104.                 //release the old camera instance
    105.                 //switch camera, from the front and the back and vice versa
    106.                 releaseCamera();
    107.                 chooseCamera();
    108.             } else {
    109.                 Toast toast = Toast.makeText(myContext, "Sorry, your phone has only one camera!", Toast.LENGTH_LONG);
    110.                 toast.show();
    111.             }
    112.         }
    113.     };
    114.     public void chooseCamera() {
    115.         //if the camera preview is the front
    116.         if (cameraFront) {
    117.             int cameraId = findBackFacingCamera();
    118.             if (cameraId >= 0) {
    119.                 //open the backFacingCamera
    120.                 //set a picture callback
    121.                 //refresh the preview
    122.                 mCamera = Camera.open(cameraId);
    123.                 mPicture = getPictureCallback();
    124.                 mPreview.refreshCamera(mCamera);
    125.             }
    126.         } else {
    127.             int cameraId = findFrontFacingCamera();
    128.             if (cameraId >= 0) {
    129.                 //open the backFacingCamera
    130.                 //set a picture callback
    131.                 //refresh the preview
    132.                 mCamera = Camera.open(cameraId);
    133.                 mPicture = getPictureCallback();
    134.                 mPreview.refreshCamera(mCamera);
    135.             }
    136.         }
    137.     }
    138.     @Override
    139.     protected void onPause() {
    140.         super.onPause();
    141.         //when on Pause, release camera in order to be used from other applications
    142.         releaseCamera();
    143.     }
    144.     private boolean hasCamera(Context context) {
    145.         //check if the device has camera
    146.         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    147.             return true;
    148.         } else {
    149.             return false;
    150.         }
    151.     }
    152.     private PictureCallback getPictureCallback() {
    153.         PictureCallback picture = new PictureCallback() {
    154.             @Override
    155.             public void onPictureTaken(byte[] data, Camera camera) {
    156.                 //make a new picture file
    157.                 File pictureFile = getOutputMediaFile();
    158.                 if (pictureFile == null) {
    159.                     return;
    160.                 }
    161.                 try {
    162.                     //write the file
    163.                     FileOutputStream fos = new FileOutputStream(pictureFile);
    164.                     fos.write(data);
    165.                     fos.close();
    166.                     Toast toast = Toast.makeText(myContext, "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG);
    167.                     toast.show();
    168.                 } catch (FileNotFoundException e) {
    169.                 } catch (IOException e) {
    170.                 }
    171.                 //refresh camera to continue preview
    172.                 mPreview.refreshCamera(mCamera);
    173.             }
    174.         };
    175.         return picture;
    176.     }
    177.     OnClickListener captrureListener = new OnClickListener() {
    178.         @Override
    179.         public void onClick(View v) {
    180.             mCamera.takePicture(null, null, mPicture);
    181.         }
    182.     };
    183.     //make picture and save to a folder
    184.     private static File getOutputMediaFile() {
    185.         //make a new file directory inside the "sdcard" folder
    186.         File mediaStorageDir = new File("/sdcard/", "JCG Camera");
    187.         //if this "JCGCamera folder does not exist
    188.         if (!mediaStorageDir.exists()) {
    189.             //if you cannot make this folder return
    190.             if (!mediaStorageDir.mkdirs()) {
    191.                 return null;
    192.             }
    193.         }
    194.         //take the current timeStamp
    195.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    196.         File mediaFile;
    197.         //and make a media file:
    198.         mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    199.         return mediaFile;
    200.     }
    201.     private void releaseCamera() {
    202.         // stop and release camera
    203.         if (mCamera != null) {
    204.             mCamera.release();
    205.             mCamera = null;
    206.         }
    207.     }
    208. }
     



    1. Download the Forums for Android™ app!


      Download

       
  2. HeavensSentSword

    HeavensSentSword Well-Known Member

    Do you want it to take higher quality pictures? From what I gathered the camera is going to take the best quality that it can. Can you compare the quality of the apps picture vs the same pic taken by the built in os?
     
  3. S12

    S12 Newbie
    Thread Starter

    yes I want it to take higher quality photos. its wayyy low res compared to the system camera. I feel the app is not using the full advantage of the phone camera. otherwise not using all the MegaPixels.
     
  4. HeavensSentSword

    HeavensSentSword Well-Known Member

    Are the pictures blurry or just pixelated?
     
  5. S12

    S12 Newbie
    Thread Starter

    I think I found the issue my app wasn't scaling the image.
     
    HeavensSentSword likes this.
  6. HeavensSentSword

    HeavensSentSword Well-Known Member

    Ah that is what I thought was happening. Good then
     
Loading...
Similar Threads - Custom Camera takes
  1. npuleio
    Replies:
    0
    Views:
    349
  2. Little_Ad 1059
    Replies:
    1
    Views:
    459
  3. Hadron
    Replies:
    6
    Views:
    599
  4. Yesi Trotamundos
    Replies:
    6
    Views:
    935
  5. Hakeem balogun
    Replies:
    0
    Views:
    504
  6. kumaranil13k
    Replies:
    7
    Views:
    719
  7. kumaranil13k
    Replies:
    11
    Views:
    754
  8. Ben Foster
    Replies:
    1
    Views:
    367
  9. Raza Shaikh
    Replies:
    1
    Views:
    833
  10. Nolan_B
    Replies:
    11
    Views:
    2,868

Share This Page

Loading...