washburn_it
Lurker
Hi,
I'm developing an app to scan QR codes (not for Covid-19) that will run on a (chinese) tablet equipped with Android 7.1.2.
The app works and I'm able to read QR codes but unfortunately the camera is rotated 90° (counterclockwise) respect to the display that is in "portrait" mode.
This tablet doesn't have options for display rotation so it is "fixed" to portrait mode.
Since (apparently) it's not possible to rotate the camera through the operating system (no option found, on the camera app neither), I tried to "force" the rotation through my app but all the examples that I found are relating to deprecated functions that are not supported anymore.
I'm using Android Studio "Bumblebee" and "targetSDK" is 25.
This is the code to initialize camera and code scanner:
How can I force rotation of the camera?
Thank you, regards.
Roberto
I'm developing an app to scan QR codes (not for Covid-19) that will run on a (chinese) tablet equipped with Android 7.1.2.
The app works and I'm able to read QR codes but unfortunately the camera is rotated 90° (counterclockwise) respect to the display that is in "portrait" mode.
This tablet doesn't have options for display rotation so it is "fixed" to portrait mode.
Since (apparently) it's not possible to rotate the camera through the operating system (no option found, on the camera app neither), I tried to "force" the rotation through my app but all the examples that I found are relating to deprecated functions that are not supported anymore.
I'm using Android Studio "Bumblebee" and "targetSDK" is 25.
This is the code to initialize camera and code scanner:
Java:
private void initialiseDetectorsAndSources()
{
String str="";
Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show();
barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(1920, 1080)
.setAutoFocusEnabled(true) //you should add this feature
.build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback()
{
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
if (ActivityCompat.checkSelfPermission(ScannerBarcodeActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
{
cameraSource.start(surfaceView.getHolder());
}
else
{
ActivityCompat.requestPermissions(ScannerBarcodeActivity.this, new
String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>()
{
@Override
public void release()
{
Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections)
{
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0)
{
String str= barcodes.valueAt(0).displayValue; /**scanned code*/
TextView txt= (TextView) findViewById(R.id.txtBarcodeValue);
txt.setText(str);
Toast.makeText(ScannerBarcodeActivity.this, str, Toast.LENGTH_LONG).show();
}
}
});
}
How can I force rotation of the camera?
Thank you, regards.
Roberto