How to capture a custom image size with the camera in android?

前端 未结 2 1241
悲哀的现实
悲哀的现实 2021-02-05 12:18

How to capture an square image in android? I want to capture an square image (such as 300x300 pixel) by calling Camera through intent in android, how can I do this?

相关标签:
2条回答
  • 2021-02-05 12:55

    Why nobody mentioned something like this?

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, xStart, yStart, 300, 300);
    

    where sourceBitmap is original capture from camera and xStart and yStart location from where cropping will start. The result should have xStart, yStart in top left corner.

    0 讨论(0)
  • 2021-02-05 13:11

    EDIT: This is deprecated since API level 21.

    Use the Camera.Size nested class

    http://developer.android.com/reference/android/hardware/Camera.Size.html

    From the android reference:

    http://developer.android.com/reference/android/hardware/Camera.html

    Class Overview

    The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.

    Make sure that the size is supported by the camera (and most probably it won't). If not, take a picture at the closest resolution and crop it or resize it.

    Camera myCamera = Camera.open(0);
    List<Camera.Size> sizes = myCamera.getPArameters().getSupportedPictureSizes();
    

    To learn about camera intents, check these questions already in SO:

    Android camera intent

    Camera intent android

    0 讨论(0)
提交回复
热议问题