Camera
The PhotoEditor SDK offers a camera implementation for Android to complement your editor, featuring basic essential camera components as well as live filters.
In order to use our camera, you need to instantiate a CameraPreviewActivity
using a CameraPreviewBuilder
and present it. You can configure the camera to fit your needs by passing a SettingsList
object to the builder. If no configuration is passed, the default setup is passed:
public class CameraDemoActivity extends Activity implements PermissionRequest.Response {
// Important permission request for Android 6.0 and above, don't forget to add this!
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionRequest.onRequestPermissionsResult(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void permissionGranted() {}
@Override
public void permissionDenied() {
/* TODO: The Permission was rejected by the user. The Editor was not opened,
* Show a hint to the user and try again. */
}
public static int PESDK_RESULT = 1;
private SettingsList createPesdkSettingsList() {
// Create a empty new SettingsList and apply the changes on this referance.
SettingsList settingsList = new SettingsList();
// If you include our asset Packs and you use our UI you also need to add them to the UI,
// otherwise they are only available for the backend
// See the specific feature sections of our guides if you want to know how to add our own Assets.
settingsList.getSettingsModel(UiConfigFilter.class).setFilterList(
FilterPackBasic.getFilterPack()
);
settingsList.getSettingsModel(UiConfigText.class).setFontList(
FontPackBasic.getFontPack()
);
settingsList.getSettingsModel(UiConfigFrame.class).setFrameList(
FramePackBasic.getFramePack()
);
settingsList.getSettingsModel(UiConfigOverlay.class).setOverlayList(
OverlayPackBasic.getOverlayPack()
);
settingsList.getSettingsModel(UiConfigSticker.class).setStickerLists(
StickerPackEmoticons.getStickerCategory(),
StickerPackShapes.getStickerCategory()
);
// Set custom camera image export settings
settingsList.getSettingsModel(CameraSettings.class)
.setExportDir(Directory.DCIM, "SomeFolderName")
.setExportPrefix("camera_");
// Set custom editor image export settings
settingsList.getSettingsModel(SaveSettings.class)
.setExportDir(Directory.DCIM, "SomeFolderName")
.setExportPrefix("result_")
.setSavePolicy(SaveSettings.SavePolicy.RETURN_ALWAYS_ONLY_OUTPUT);
return settingsList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openCamera();
}
private void openCamera() {
SettingsList settingsList = createPesdkSettingsList();
new CameraPreviewBuilder(this)
.setSettingsList(settingsList)
.startActivityForResult(this, PESDK_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PESDK_RESULT) {
// Editor has saved an Image.
Uri resultURI = data.getParcelableExtra(ImgLyIntent.RESULT_IMAGE_URI);
Uri sourceURI = data.getParcelableExtra(ImgLyIntent.SOURCE_IMAGE_URI);
// Scan result uri to show it up in the Gallery
if (resultURI != null) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).setData(resultURI));
}
// Scan source uri to show it up in the Gallery
if (sourceURI != null) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).setData(sourceURI));
}
Log.i("PESDK", "Source image is located here " + sourceURI);
Log.i("PESDK", "Result image is located here " + resultURI);
// TODO: Do something with the result image
// OPTIONAL: read the latest state to save it as a serialisation
SettingsList lastState = data.getParcelableExtra(ImgLyIntent.SETTINGS_LIST);
try {
new PESDKFileWriter(lastState).writeJson(new File(
Environment.getExternalStorageDirectory(),
"serialisationReadyToReadWithPESDKFileReader.json"
));
} catch (IOException e) { e.printStackTrace(); }
} else if (resultCode == RESULT_CANCELED && requestCode == PESDK_RESULT) {
// Editor was canceled
Uri sourceURI = data.getParcelableExtra(ImgLyIntent.SOURCE_IMAGE_URI);
// TODO: Do something...
}
}
}
class KCameraDemoActivity : Activity(), PermissionRequest.Response {
companion object {
const val PESDK_RESULT = 1
}
// Important permission request for Android 6.0 and above, don't forget to add this!
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
PermissionRequest.onRequestPermissionsResult(requestCode, permissions, grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun permissionGranted() {}
override fun permissionDenied() {
/* TODO: The Permission was rejected by the user. The Editor was not opened,
* Show a hint to the user and try again. */
}
// Create a empty new SettingsList and apply the changes on this referance.
// If you have included our asset Packs and you want to use our default UI you also need to add them to the UI config,
// otherwise they are only available for the backend link serialisation.
// See the specific feature sections of our guides if you want to know how to add your own assets.
private fun createPesdkSettingsList() = PhotoEditorSettingsList()
.configure<UiConfigFilter> {
it.setFilterList(FilterPackBasic.getFilterPack())
}
.configure<UiConfigText> {
it.setFontList(FontPackBasic.getFontPack())
}
.configure<UiConfigFrame> {
it.setFrameList(FramePackBasic.getFramePack())
}
.configure<UiConfigOverlay> {
it.setOverlayList(OverlayPackBasic.getOverlayPack())
}
.configure<UiConfigSticker> {
it.setStickerLists(
StickerPackEmoticons.getStickerCategory(),
StickerPackShapes.getStickerCategory()
)
}.configure<CameraSettings> {
// Set custom camera image export settings
it.setExportDir(Directory.DCIM, "SomeFolderName")
it.setExportPrefix("camera_")
}
.configure<SaveSettings> {
// Set custom editor image export settings
it.setExportDir(Directory.DCIM, "SomeFolderName")
it.setExportPrefix("result_")
it.savePolicy = SaveSettings.SavePolicy.RETURN_ALWAYS_ONLY_OUTPUT
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
openCamera()
}
private fun openCamera() {
val settingsList = createPesdkSettingsList()
CameraPreviewBuilder(this)
.setSettingsList(settingsList)
.startActivityForResult(this, PESDK_RESULT)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == PESDK_RESULT) { // Editor has saved an Image.
val resultURI = data.getParcelableExtra<Uri?>(ImgLyIntent.RESULT_IMAGE_URI)?.also {
// Scan result uri to show it up in the Gallery
sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).setData(it))
}
val sourceURI = data.getParcelableExtra<Uri?>(ImgLyIntent.SOURCE_IMAGE_URI)?.also {
// Scan source uri to show it up in the Gallery
sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).setData(it))
}
Log.i("PESDK", "Source image is located here $sourceURI")
Log.i("PESDK", "Result image is located here $resultURI")
// TODO: Do something with the result image
// OPTIONAL: read the latest state to save it as a serialisation
val lastState = data.getParcelableExtra<SettingsList>(ImgLyIntent.SETTINGS_LIST)
try {
PESDKFileWriter(lastState).writeJson(File(
Environment.getExternalStorageDirectory(),
"serialisationReadyToReadWithPESDKFileReader.json"
))
} catch (e: IOException) {
e.printStackTrace()
}
} else if (resultCode == Activity.RESULT_CANCELED && requestCode == PESDK_RESULT) {
// Editor was canceled
val sourceURI = data.getParcelableExtra<Uri?>(ImgLyIntent.SOURCE_IMAGE_URI)
// TODO: Do something...
}
}
}
Disable editor to open
To prevent the ‘editor screen’ to open after taking a photo and to stay on ‘capture mode’ use this code:
settingsList.getSettingsModel(CameraSettings.class).setOpenEditorAfterCapture(false);
For more details, take a look at our getting started section.