#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#BridgeLogger: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private NativeMe As JavaObject
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")
If FirstTime Then
NativeMe.InitializeContext
USBdetails.Initialize
End If
End Sub
Sub Activity_Resume
Dim c1 As Int = NativeMe.RunMethod("camera1CameraCount", Null)
Log("With Camera1: "&c1)
Dim c2 As Int = NativeMe.RunMethod("camera2CameraCount", Null)
Log("With Camera2: "&c2)
USBdetails.GetDevices
Log("USB devices: "&USBdetails.NumberOfDevices)
Dim a As Int
For a = 0 To USBdetails.NumberOfDevices-1
Log("USB "&a&": DeviceClass = "&USBdetails.devices(a).DeviceClass)
Log("USB "&a&": DeviceId = "&USBdetails.devices(a).DeviceId)
Log("USB "&a&": DeviceName = "&USBdetails.devices(a).DeviceName)
Log("USB "&a&": DeviceSubclass = "&USBdetails.devices(a).DeviceSubclass)
Log("USB "&a&": InterfaceCount = "&USBdetails.devices(a).InterfaceCount)
Log("USB "&a&": ProductId = "&USBdetails.devices(a).ProductId)
Log("USB "&a&": VendorId = "&USBdetails.devices(a).VendorId)
Dim tmpS As String = USBdetails.devices(a).DeviceId
Dim c3 As String = NativeMe.RunMethod("openUSBCamera", Array(tmpS))
Log("Opening Camera USB "&a&" result :"&c3)
Next
Dim c3 As String = NativeMe.RunMethod("openUSBCamera", Array("0"))
Log("Opening Camera 0 result :"&c3)
Dim c3 As String = NativeMe.RunMethod("openUSBCamera", Array("1"))
Log("Opening Camera 1 result :"&c3)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
#If Java
import android.hardware.Camera;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CameraCharacteristics;
import java.util.List;
import java.util.ArrayList;
import android.view.Surface;
import android.graphics.SurfaceTexture;
import android.util.Size;
import android.widget.Toast;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.CameraInfo;
import android.content.pm.PackageManager;
import android.content.Context;
CameraCaptureSession mSession;
CaptureRequest.Builder mBuilder;
CameraDevice mCameraDevice;
CameraManager mCameraManager;
public int camera1CameraCount() {
try {
int numberOfCameras = Camera.getNumberOfCameras();
return numberOfCameras;
} catch (Exception e) {
return -1;
}
}
public int camera2CameraCount() {
CameraManager manager;
manager=(CameraManager)getSystemService(Context.CAMERA_SERVICE);
try {
return manager.getCameraIdList().length;
} catch (CameraAccessException e) {
return -1;
}
}
public String openUSBCamera(String CameraID) {
CameraManager manager;
manager=(CameraManager)getSystemService(Context.CAMERA_SERVICE);
try {
manager.openCamera(CameraID, new MyCameraDeviceStateCallback(), null);
return "OK";
} catch (Exception e) {
Context context = getApplicationContext();
String errorMessage = e.getMessage();
Toast.makeText(context,errorMessage, Toast.LENGTH_SHORT).show();
return "Fail";
}
}
private Size getSmallestSize(String cameraId) throws CameraAccessException {
/*
Size[] outputSizes = mCameraManager.getCameraCharacteristics(cameraId)
.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(SurfaceTexture.class);
if (outputSizes == null || outputSizes.length == 0) {
throw new IllegalStateException(
"Camera " + cameraId + "doesn't support any outputSize.");
}
Size chosen = outputSizes[0];
for (Size s : outputSizes) {
if (chosen.getWidth() >= s.getWidth() && chosen.getHeight() >= s.getHeight()) {
chosen = s;
}
}
*/
Size chosen = Size.parseSize("3*+6");
return chosen;
}
class MyCameraDeviceStateCallback extends CameraDevice.StateCallback {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
//get builder
try {
mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
//flash on, default is on
mBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AF_MODE_AUTO);
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
List<Surface> list = new ArrayList<Surface>();
SurfaceTexture mSurfaceTexture = new SurfaceTexture(1);
Size size = getSmallestSize(mCameraDevice.getId());
mSurfaceTexture.setDefaultBufferSize(size.getWidth(), size.getHeight());
Surface mSurface = new Surface(mSurfaceTexture);
list.add(mSurface);
mBuilder.addTarget(mSurface);
camera.createCaptureSession(list, new MyCameraCaptureSessionStateCallback(), null);
} catch (CameraAccessException e) {
Context context = getApplicationContext();
String errorMessage = e.getMessage();
Toast.makeText(context,errorMessage, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
}
@Override
public void onError(CameraDevice camera, int error) {
}
}
class MyCameraCaptureSessionStateCallback extends CameraCaptureSession.StateCallback {
@Override
public void onConfigured(CameraCaptureSession session) {
mSession = session;
try {
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (CameraAccessException e) {
Context context = getApplicationContext();
String errorMessage = e.getMessage();
Toast.makeText(context,errorMessage, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}
#End If