A compatibility-strong Android camera call scheme

Source: Internet
Author: User

Strong definition of compatibility
    1. Camera Pixel Independent
    2. Resolution Independent
    3. Screen Orientation Independent
Camera Pixel Independent

Pixel independent now, regardless of the pixel geometry of the camera, I can get a relatively suitable photo. Let's say the fit here means that the size of the photo you need is consistent with the size of the data the camera gets.

float scale = expectedHeight / expectedWidth;List<Camera.Size> pictureSize = params.getSupportedPictureSizes();for (Camera.Size size:pictureSize)    if (Math.abs(size.height/size.width - scale)<=0.01) {        params.setPictureSize(size.width, size.height);        break;    }

This way, you can guarantee that the length-to-width ratio of the captured photo is correct and not stretched. Of course for循环 the conditions inside determine how to write personal likes.

Resolution Independent

Here the resolution is irrelevant and is about previewSize参数 the setting. Suppose the width and height of the preview form are the viewWidth and viewHeight , we take the nearest value.

List<Camera.Size> sizes = params.getSupportedPreviewSizes();for (Camera.Size size : sizes) {    if (Math.abs(size.height-viewHeight)<10 && Math.abs(size.width-viewWidth)<10) {        params.setPreviewSize(size.width, size.height);        break;    }

This ensures that the picture is not stretched and deformed during the preview.

Screen Orientation Independent

is mostly set DisplayOrientation参数 . In the case of a horizontal screen app, it must be declared in the Activity tab in Androidmanifest.xml android:screenOrientation="landscape" . In the case of a vertical screen, the androidmanifest.xml does not need to be set, but two parameters need to be set:

params.setRotation(90);camera.setDisplayOrientation(90);

The first parameter is the orientation of the photo when the image is set, corresponding to the PictureCallback image orientation of the byte[in. The second parameter is to set the orientation of the preview interface, where the diagram is easy to write 90 degrees.

But note:

Regardless of how the parameter is set, the onPreviewFrame() orientation of the image in Previewcallback is necessarily the direction of the horizontal screen, which cannot be changed.

If you need to get images in the right direction in real time, do this:

@Overridepublic void onPictureTaken(byte[] data, Camera camera) {    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);    Matrix matrix = new Matrix();    matrix.preRotate(90);    bitmap = Bitmap.createBitmap(bitmap ,0,0, bitmap .getWidth(), bitmap .getHeight(),matrix,true);};

But it is highly recommended not to do so! Efficiency is a big problem. Assuming that the camera set FPS is 24,1080p resolution, then the amount of data transmitted in a second is about 1080 * 1920 * 8 * 24, specifically not forget, anyway, is a very large number, but also a rotation, which for mobile hardware resources is simply to find death.

Video recording

In fact, it's almost the same as getting a picture in front of you, but it's just a mediarecorder. are also settings for some parameters. Don't dwell on it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.