v1.0.0-beta3.10
v1.0.0-beta3.10v1.0.0-beta3.9v0.13.2

CameraKit Documentation - v1.0.0-beta3.10

  1. Setup
  2. Basic Usage
  3. Capturing Images
  4. Attributes
  5. Permissions
  6. Event Listeners
  7. Additional Notes

Setup

Add CameraKit and Kotlin to the dependencies section of your build.gradle.

dependencies {
    implementation 'com.camerakit:camerakit:1.0.0-beta3.10'
    implementation 'com.camerakit:jpegkit:0.1.0'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
}

Basic Usage

CameraKit relies on a CameraKitView in the XML layout.

<com.camerakit.CameraKitView
    android:id="@+id/camera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"/>

Initialize the CameraKitView in the Java activity. v1.0.0-beta3.10 requires five methods to be overloaded; four handling state, and one handling permissions.

public class MainActivity extends AppCompatActivity {
    private CameraKitView cameraKitView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraKitView = findViewById(R.id.camera);
    }

    @Override
    protected void onStart() {
        super.onStart();
        cameraKitView.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraKitView.onResume();
    }

    @Override
    protected void onPause() {
        cameraKitView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        cameraKitView.onStop();
        super.onStop();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

The CameraKitView will exist in your activity and display the output of the Camera Preview. See all options in the Attributes section.

Capturing Images

Capturing images is done through the cameraKitView.captureImage callback

cameraKitView.captureImage(new  CameraKitView.ImageCallback() {
    @Override
    public void onImage(CameraKitView cameraKitView, final byte[] capturedImage) {
        // capturedImage contains the image from the CameraKitView.
    }
})

Example

// From button OnClickListener
private View.OnClickListener photoOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        cameraKitView.captureImage(new CameraKitView.ImageCallback() {
            @Override
            public void onImage(CameraKitView cameraKitView, final byte[] capturedImage) {
                File savedPhoto = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
                try {
                    FileOutputStream outputStream = new FileOutputStream(savedPhoto.getPath());
                    outputStream.write(capturedImage);
                    outputStream.close();
                } catch (java.io.IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
};

Attributes

setFacing

Format Method
XML app:camera_facing="back"
Java cameraKitView.setFacing(CameraKit.FACING_BACK)

Possible values:

  • back, FACING_BACK
  • front, FACING_FRONT

toggleFacing

Format Method
XML none
Java cameraKitView.toggleFacing()

setFlash

Format Method
XML app:camera_flash="off"
Java cameraKitView.setFlash(CameraKit.FLASH_OFF)

Possible values:

  • off, FLASH_OFF
  • on, FLASH_ON

setFocus

Format Method
XML app:camera_focus="auto"
Java cameraKitView.setFocus(CameraKit.FOCUS_AUTO)

Possible values:

  • auto, FOCUS_AUTO
  • continuous, FOCUS_CONTINUOUS
  • off, FOCUS_OFF

zoomFactor

Format Method
XML app:camera_zoomFactor="1"
Java cameraKitView.setZoomFactor(1.0f)

Possible values:

  • float: [0,100]

aspectRatio

Format Method
XML app:camera_aspectRatio="1"
Java cameraKitView.setAspectRatio(1f)

Possible values:

  • float: [0,inf]

imageMegaPixels

Format Method
XML app:camera_imageMegaPixels="2"
Java cameraKitView.setImageMegaPixels(2f)

Possible values:

  • float: [0,100]

imageJpegQuality

Format Method
XML app:camera_imageJpegQuality="100"
Java none

Possible values:

  • int: [0,100]

sensorPreset

Format Method
XML none
Java cameraKitView.setSensorPreset(CameraKit.SENSOR_PRESET_NONE)

Possible values:

  • SENSOR_PRESET_NONE
  • …ACTION, PORTRAIT, LANDSCAPE, NIGHT, NIGHT_PORTRAIT, THEATRE, BEACH, SNOW, SUNSET, STEADYPHOTO, FIREWORKS, SPORTS, PARTY, CANDLELIGHT, BARCODE

previewEffect

Format Method
XML none
Java cameraKitView.setPreviewEffect(CameraKit.PREVIEW_EFFECT_NONE)

Possible values:

  • PREVIEW_EFFECT_NONE
  • …MONO, SOLARIZE, SEPIA, POSTERIZE, WHITEBOARD, BLACKBOARD, AQUA

setAdjustViewBounds

setAdjustViewBounds(Boolean adjustViewBounds)

Possible parameters:

  • Boolean

Permissions

permissions

Format Method
XML app:permisisons="camera"
Java cameraKitView.setPermissions()

Possible values:

  • camera
  • all
  • audio
  • location
  • storage
  • none

requestPermissions

cameraKitView.requestPermissions(Activity)

Possible parameters:

  • Activity

onRequestPermissionsResult

cameraKitView.onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

Event Listeners

GestureListener

cameraKitView.setGestureListener(new CameraKitView.GestureListener() {
    @Override
    public void onTap(CameraKitView cameraKitView, float v, float v1) {

    }

    @Override
    public void onLongTap(CameraKitView cameraKitView, float v, float v1) {

    }

    @Override
    public void onDoubleTap(CameraKitView cameraKitView, float v, float v1) {

    }

    @Override
    public void onPinch(CameraKitView cameraKitView, float v, float v1, float v2) {

    }
});

CameraListener

cameraKitView.setCameraListener(new CameraKitView.CameraListener() {
    @Override
    public void onOpened() {

    }

    @Override
    public void onClosed() {

    }
});

PreviewListener

cameraKitView.setPreviewListener(new CameraKitView.PreviewListener() {
    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }
});

ErrorListener

cameraKitView.setErrorListener(new CameraKitView.ErrorListener() {
    @Override
    public void onError(CameraKitView cameraKitView, CameraKitView.CameraException e) {

    }
});

Additional Notes and Common Issues

onConfigurationChanged

In beta3.10 setting onConfigurationChanged to watch for screen size changes in AndroidManifest.xml will produce unexpected output when rotating the deivce on Android version 7.0 or higher.

If you encounter issues with rotation, first ensure the following line is NOT in your AndroidManifest.xml.

<activity>
    android:configChanges="orientation|screenLayout"
</activity>