D T
D T

Reputation: 3756

How setting Camera2 API the standard camera?

If I use standard camera via Intent to capture image:

Open Camera:

 val takePicture = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePicture.resolveActivity(packageManager) != null) {
            startActivityForResult(takePicture, TAKE_PICTURE)
        }

Camera Preview

Camera Preview

Result display on ImageView

Result Image View

If I use Camera2 API at: git: https://github.com/googlesamples/android-Camera2Basic Result of Image not display portrait

Camera 2 Preview

Camera2 Preview

Result display on ImageView

Image View

My code display Image:

val bitmap = BitmapFactory.decodeFile(file.toString())
  imagePreview.setImageBitmap(bitmap)
                        }

How setting Camera2 API to display result the same standard camera?

Upvotes: 0

Views: 568

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

In Camera2Basic example, the ImageSaver does not rotate the captured JPEG with regards to device orientation. Instead, Camera2BasicFragment.captureStillPicture() sets CaptureRequest.JPEG_ORIENTATION, which is only a recommendation for the camera firmware.

Camera devices may either encode this value into the JPEG EXIF header, or rotate the image data to match this orientation. When the image data is rotated, the thumbnail data will also be rotated.

Most often, this recommendation 'only' sets the header, but some devices miss even that. See a recent article on this feature and its reliability.

Please note that the EXIF orientation tag is not respected by all viewer software, therefore often the stock Camera applications do rotate the actual JPEG to default orientation.

Your code that loads the captured picture to ImageView currently ignores this tag. You can use ExifInterface.getAttributeInt(TAG_ORIENTATION) to extract the orientation from the file or input stream. Or, if you capture an image and immediately display it, you can get device orientation directly from the sensor. Now it's time to decide if the camera stored the image as portrait (i.e. width is smaller than height), or as landscape, in which case it's your duty to rotate it for display. Don't rotate the bitmap according to this orientation. Instead, you can call imagePreview.setImageMatrix() to display the image correctly.

By the way, please don't decode the JPEG to full-scale bitmap in memory if you only need it to be passed to your ImageView: this may consume too much RAM. The easiest one-liner is to call setImageURI() instead.

Upvotes: 2

Related Questions