1

Currently I'm using a custom camera application, the preview looks just fine. But when I take the picture and display it in my other activity the picture has decreased with like 80%. Anyone knows why this is happening? Also in gallery the quality is poor. I'm using the Camera API Demo from android.

My parameters :

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    parameters = mCamera.getParameters();


    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    parameters.setJpegQuality(100);
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    parameters.setRotation(90);
    Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if (display.getRotation() == Surface.ROTATION_0) {
        mCamera.setDisplayOrientation(90);
    } else if (display.getRotation() == Surface.ROTATION_270) {
        mCamera.setDisplayOrientation(180);
    }

    mCamera.setParameters(parameters);

    mCamera.startPreview();
}

Code saving image:

private PictureCallback mPicture = new PictureCallback() {
    private String TAG = "DocsPro";

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.d(TAG, "Error creating media file, check storage permissions : PICTURE FILE IS NULL");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            String filepath = pictureFile.getAbsolutePath();
            Intent edit = new Intent(TakePhoto.this, EditPhoto.class);
            edit.putExtra("filepath", filepath);
            startActivity(edit);
            finish();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "DocsPro");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}
user3678528
  • 1,741
  • 2
  • 18
  • 24
Jordi Sipkens
  • 595
  • 1
  • 9
  • 25

1 Answers1

7

Problem Solved : I just put this in the surface changed on my preview class.

 List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
    Camera.Size size = sizes.get(0);
    for(int i=0;i<sizes.size();i++)
    {
        if(sizes.get(i).width > size.width)
            size = sizes.get(i);
    }
    parameters.setPictureSize(size.width, size.height);

And then just resize the bitmap because the picture was to big :

    imageView = (ImageView) findViewById(R.id.Image);
    Bitmap bp = BitmapFactory.decodeFile(imagePath);
    Bitmap resized = Bitmap.createScaledBitmap(bp,(int)(bp.getWidth()*0.7), (int)(bp.getHeight()*0.7), true);
    imageView.setImageBitmap(resized);
Jordi Sipkens
  • 595
  • 1
  • 9
  • 25
  • Thanks working for me. My captured image is much clear now. Also to get a clear preview of camera do params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); See link [Auto Focus preview](https://stackoverflow.com/questions/15623944/how-to-autofocus-android-camera-automatically) – Ashutosh Chamoli Mar 22 '18 at 19:08