This question already has answers here:
Zxing Camera in Portrait mode on Android
(9 answers)
Closed 9 years ago.
i've succeeded using the ZXing barcode scanning library , but only on landscape mode.
i've also succeeded setting the camera preview to be on portrait mode AND show it correctly (without stretching) , but now the barcode doesn't work at all .
here're the changes i've made to "setDesiredCameraParameters" on "CameraConfigurationManager.java" in order to show the camera correctly :
void setDesiredCameraParameters(Camera camera)
{
Camera.Parameters parameters = camera.getParameters();
Log.d(TAG, "Setting preview size: " + cameraResolution);
setFlash(parameters);
setZoom(parameters);
camera.setDisplayOrientation(90);
parameters.set("rotation", 90);
parameters.setPreviewSize(cameraResolution.y, cameraResolution.x);
camera.setParameters(parameters);
}
i've tried some solutions mentioned on other places , but either they don't work , or they work but cannot show the camera preview correctly .
examples:
How to use Zxing in portrait mode?
http://code.google.com/p/zxing/issues/detail?id=178#c46
https://github.com/pplante/zxing-android/issues
when i'm finished with that , i also need to customize the location&size of the rectangle to the scanning . i know that i need to change "setManualFramingRect" on "CameraManager.java" , but i'm not sure if i do it correctly . here's the code for that:
public void setManualFramingRect(Rect rect)
{
if (initialized)
{
Point screenResolution = configManager.getScreenResolution();
if (rect.right >= screenResolution.x)
rect.right = screenResolution.x - 1;
if (rect.left < 0)
rect.left = 0;
if (rect.bottom >= screenResolution.y)
rect.bottom = screenResolution.y - 1;
if (rect.top < 0)
rect.top = 0;
framingRect = rect;
Log.d(TAG, "Calculated manual framing rect: " + framingRect);
framingRectInPreview = null;
}
else
_requestedFramingRect = new Rect(rect);
}
of course ,i've changed "openDriver" to call :
if (_requestedFramingRect != null)
setManualFramingRect(_requestedFramingRect);
please help me.
EDIT: now i've found out that it doesn't work on some devices . it crashes on the beginning , and if you debug , you can see that even the preview doesn't work well.
There's more to it than this. For example you need to actually "rotate" the camera data (or, scan it as if it's vertical) when the camera orientation is not the same as the device orientation. And when using a front camera, you need to account for the fact that its rotation is reversed.
Related
I have been updated my camera feature from Camera to camera2 api.
When i capture the image with front camera and display it in imageview. the orientation of the image is changed. Then i used this code to change the orientation of the image.
int jpegOrientation =
(ORIENTATIONS.get(rotation) + characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) + 270) % 360;
capturebuilder.set(CaptureRequest.JPEG_ORIENTATION, jpegOrientation);
Above code is working for android version(i.e version 5,9) which i have tested. When i run the same code in Android version 10 it does not work. Below is the image
please,any one can help me out for this issue
Did you try the solution below :
https://medium.com/#kenodoggy/solving-image-rotation-on-android-using-camera2-api-7b3ed3518ab6
private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) {
if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0;
int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION);
// Round device orientation to a multiple of 90
deviceOrientation = (deviceOrientation + 45) / 90 * 90;
// Reverse device orientation for front-facing cameras
boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
if (facingFront) deviceOrientation = -deviceOrientation;
// Calculate desired JPEG orientation relative to camera orientation to make
// the image upright relative to the device orientation
int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360;
return jpegOrientation;
}
to know more details follow this post: https://medium.com/#kenodoggy/solving-image-rotation-on-android-using-camera2-api-7b3ed3518ab6
can somebody help me out with the following code. I want to transform the HdrViewFinder project from google samples (https://github.com/googlesamples/android-HdrViewfinder) so that it also works in portrait mode. But the project works only in landscape mode.
In portrait mode, the camera preview gets distorted.
So, here is the method I extracted from the HdrViewfinderActivity.java class which I think is responsible for setting the whole view into landscape:
/**
* Configure the surfaceview and RS processing.
*/
private void configureSurfaces() {
// Find a good size for output - largest 16:9 aspect ratio that's less than 720p
final int MAX_WIDTH = 1280;
final float TARGET_ASPECT = 16.f / 9.f;
final float ASPECT_TOLERANCE = 0.1f;
StreamConfigurationMap configs =
mCameraInfo.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (configs == null) {
throw new RuntimeException("Cannot get available picture/preview sizes.");
}
Size[] outputSizes = configs.getOutputSizes(SurfaceHolder.class);
Size outputSize = outputSizes[0];
float outputAspect = (float) outputSize.getWidth() / outputSize.getHeight();
for (Size candidateSize : outputSizes) {
if (candidateSize.getWidth() > MAX_WIDTH) continue;
float candidateAspect = (float) candidateSize.getWidth() / candidateSize.getHeight();
boolean goodCandidateAspect =
Math.abs(candidateAspect - TARGET_ASPECT) < ASPECT_TOLERANCE;
boolean goodOutputAspect =
Math.abs(outputAspect - TARGET_ASPECT) < ASPECT_TOLERANCE;
if ((goodCandidateAspect && !goodOutputAspect) ||
candidateSize.getWidth() > outputSize.getWidth()) {
outputSize = candidateSize;
outputAspect = candidateAspect;
}
}
Log.i(TAG, "Resolution chosen: " + outputSize);
// Configure processing
mProcessor = new ViewfinderProcessor(mRS, outputSize);
setupProcessor();
// Configure the output view - this will fire surfaceChanged
mPreviewView.setAspectRatio(outputAspect);
mPreviewView.getHolder().setFixedSize(outputSize.getWidth(), outputSize.getHeight());
}
How could I change this method so that it works also in portrait mode? What do I need to change ?
Setting only the screenOrientation attribute in the manifest file to portrait did not help.
I have found out the 16:9 is used for full landscape mode and that 9:16 used for full portrait mode. So, I changed MAX_WIDTH to 720 and TARGET_ASPECT to 9.f/16.f . But that also did not help.
Can somebody provide a solution ?
Here is a little sketch showing the problem/what happens when I just set the screenOrientation attribute in the manifest file to portrait mode:
I have an experimental fork that achieves this, by switching to TextureView from SurfaceView.
tested on Sony G8441 a.k.a Xperia XZ1 Compact with Android Pie
I am using android camera2 in my application to take continuous images, Here when I use camera2 getting image preview brightness very dark compare to original camera. I seen this but there is no similar requirement in that answer.
I tried to set brightness in camera2 as suggested here:
Note that this control will only be effective if android.control.aeMode != OFF. This control will take effect even when android.control.aeLock == true.
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, true);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, 6);
But it still showing preview as dark image only as shown below.
See the difference here:
Original Camera:
Using Camera2:
And what is the value I need to pass as second parameter in:
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, 6);
I kept 6 because as suggested in doc's:
For example, if the exposure value (EV) step is 0.333, '6' will mean an exposure compensation of +2 EV; -3 will mean an exposure compensation of -1 EV.
But still no effect in brightness..
Here it is:
Add below code in onConfigured() and unlockFocus()
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,getRange());
By using the above code you will get the better preview. But your captured picture will remain as it is. To get the better picture as well use the same below code in captureStillPicture()
captureBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, getRange());
getRange is:
private Range<Integer> getRange() {
CameraCharacteristics chars = null;
try {
chars = mCameraManager.getCameraCharacteristics(mCameraId);
Range<Integer>[] ranges = chars.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
Range<Integer> result = null;
for (Range<Integer> range : ranges) {
int upper = range.getUpper();
// 10 - min range upper for my needs
if (upper >= 10) {
if (result == null || upper < result.getUpper().intValue()) {
result = range;
}
}
}
if (result == null) {
result = ranges[0];
}
return result;
} catch (CameraAccessException e) {
e.printStackTrace();
return null;
}
}
CONTROL_AE_LOCK should be off. You have misinterpreted the doc, possibly document itself is a bit confusing.
Note that this control will only be effective if
android.control.aeMode != OFF. This control will take effect even when
android.control.aeLock == true.
What it means is that when AE lock is ON, the exposure compensation will be applied on the locked exposure and not on the instantaneous exposure at the time of taking picture.
Even in your repeat request, exposure is locked so it doesn't help.
Remove AE lock and it should work.
While setting CONTROL_AE_EXPOSURE_COMPENSATION the second parameter as defined by docs is relative to CameraCharacteristics.CONTROL_AE_COMPENSATION_STEP
The adjustment is measured as a count of steps, with the step size defined by android.control.aeCompensationStep and the allowed range by android.control.aeCompensationRange."
The value of 6 in the example for +2EV is correct only when step is 0.333 which is just an example.
Following code will give you the exposure compensation value to be used for +2EV
CameraManager manager = (CameraManager)this.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
double exposureCompensationSteps = characteristics.get(CameraCharacteristics.CONTROL_AE_COMPENSATION_STEP).doubleValue();
int exposureCompensation = (int)( 2.0 / exposureCompensationSteps );
I would also suggest you check if the value is within the range specified by CameraCharacteristics.CONTROL_AE_COMPENSATION_RANGE
You can try this
public void setBrightness(int value) {
int brightness = (int) (minCompensationRange + (maxCompensationRange - minCompensationRange) * (value / 100f));
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, brightness);
applySettings();
}
private void applySettings() {
captureSession.setRepeatingRequest(previewRequestBuilder.build(), null, null);
}
I messed around with CaptureRequest.SENSOR_SENSITIVITY and it worked great on my Samsung s3, s7 and s8 phones.
You can get the CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE
sensitivity_range = chars.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
On my s7, the range is from mid 50s to more than 3000. I then set it to 1500 as follows.
mCaptureRequest.set(CaptureRequest.SENSOR_SENSITIVITY, 1500);
It brightened the preview a few factors.
First, don't lock autoexposure - that's not needed when adjusting exposure compensation.
Second, did you call CameraCaptureSession.setRepeatingRequest with your new capture request?
I have a Module to put cartoon face on eyes or anywhere else on live Camera Preview. I am using Moodme Sdk. I have implemented camera preview. I m getting landmark x and y axis value. But I don't know where do i add those landmark and how to put that image on eyes using landmark. This is code for while getting person face on live camera.
#Override
public void onImageAvailable(ImageReader reader) {
Image image = imageReader.acquireLatestImage();
if (image == null) {
return;
}
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
byte[] yBytes = new byte[yBuffer.remaining()];
yBuffer.get(yBytes);
ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
byte[] uBytes = new byte[uBuffer.remaining()];
uBuffer.get(uBytes);
ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
byte[] vBytes = new byte[vBuffer.remaining()];
vBuffer.get(vBytes);
tracker.processImageBuffer(yBytes, WIDTH, HEIGHT, WIDTH, MDMTrackerManager.FrameFormat.GRAY);
//renderer.updateTextureImage(yBytes, uBytes, vBytes, image.getPlanes()[1].getPixelStride());
image.close();
if (tracker.isFaceTracked()) {
// renderer.updateVertices();
}
if (tracker.isFaceTracked()) {
// translate to opengl coordinates
float[] landmarks = new float[66*2];
for (int i = 0; i < 66; ++i) {
if(i >=17 && i <27 || i >=36 && i <48 ) {
landmarks[2 * i] = 1.0f - tracker.getLandmarks()[2 * i] / (HEIGHT / 2);
landmarks[2 * i + 1] = 1.0f - tracker.getLandmarks()[2 * i + 1] / (WIDTH / 2);
}
}
// renderer.updateLandmarks(landmarks);
} else {
// renderer.updateLandmarks(null);
}
long currentTime = System.currentTimeMillis();
double fps = 1000.0 / (currentTime - lastFrameTime);
updater.update(fps);
lastFrameTime = currentTime;
}
I have also used Face Detection Library But that is not giving me accurate result.Is There any good Library For Face detection and put image or Mask on Camera Preview. Any help will be appreciated.
There are many libraries available which add a face mask on camera preview. Almost all of them use OpenCV. Check out these libraries.
FaceFilter
Face Replace
FaceTracker
Android GPUimage
The Android GPUimage seems to add image on Camera Preview. A similar question used this library to add face mask on camera preview. You can take a look into the answer posted on the question.
The FaceFilter library does the same work, but on a captured image. However you can see the tutorial for the library posted by the author and integrate it with face detection. There are several tutorials for face detection. This tutorial explains how to implement face detection, while also overlaying graphics on it. Although there is not much on the overlaid graphics in the tutorial, it might solve your question.
I'm making a line follower for my robot on Android (to learn Java/Android programming), currently I'm facing the image processing problem: the camera preview returns an image format called YUV which I want to convert to a threshold in order to know where the line is, how would one do that?
As of now I've succeeded getting something, that is I definitely can read data from the camera preview and by some miracle even know if the light intensity is over or under a certain value at a certain area on the screen. My goal is to draw the robot's path on an overlay over the camera's preview, that too works to some extent, but the problem is the YUV management.
As you can see not only the dark area is drawn sideways, but it also repeats itself 4 times and the preview image is stretched, I cannot figure out how to fix these problems.
Here's the relevant part of code:
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
// camera setup
mCamera = Camera.open();
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
for(int i=0; i<sizes.size(); i++)
{
Log.i("CS", i+" - width: "+sizes.get(i).width+" height: "+sizes.get(i).height+" size: "+(sizes.get(i).width*sizes.get(i).height));
}
// change preview size
final Camera.Size cs = sizes.get(8);
parameters.setPreviewSize(cs.width, cs.height);
// initialize image data array
imgData = new int[cs.width*cs.height];
// make picture gray scale
parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(parameters);
// change display size
LayoutParams params = (LayoutParams) mSurfaceView.getLayoutParams();
params.height = (int) (mSurfaceView.getWidth()*cs.height/cs.width);
mSurfaceView.setLayoutParams(params);
LayoutParams overlayParams = (LayoutParams) swOverlay.getLayoutParams();
overlayParams.width = mSurfaceView.getWidth();
overlayParams.height = mSurfaceView.getHeight();
swOverlay.setLayoutParams(overlayParams);
try
{
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
catch (IOException e)
{
e.printStackTrace();
mCamera.stopPreview();
mCamera.release();
}
// callback every time a new frame is available
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera)
{
// create bitmap from camera preview
int pixel, pixVal, frameSize = cs.width*cs.height;
for(int i=0; i<frameSize; i++)
{
pixel = (0xff & ((int) data[i])) - 16;
if(pixel < threshold)
{
pixVal = 0;
}
else
{
pixVal = 1;
}
imgData[i] = pixVal;
}
int cp = imgData[(int) (cs.width*(0.5+(cs.height/2)))];
//Log.i("CAMERA", "Center pixel RGB: "+cp);
debug.setText("Center pixel: "+cp);
// process preview image data
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
int start, finish, last;
start = finish = last = -1;
float x_ratio = mSurfaceView.getWidth()/cs.width;
float y_ratio = mSurfaceView.getHeight()/cs.height;
// display calculated path on overlay using canvas
Canvas overlayCanvas = overlayHolder.lockCanvas();
overlayCanvas.drawColor(0, Mode.CLEAR);
// start by finding the tape from bottom of the screen
for(int y=cs.height; y>0; y--)
{
for(int x=0; x<cs.width; x++)
{
pixel = imgData[y*cs.height+x];
if(pixel == 1 && last == 0 && start == -1)
{
start = x;
}
else if(pixel == 0 && last == 1 && finish == -1)
{
finish = x;
break;
}
last = pixel;
}
//overlayCanvas.drawLine(start*x_ratio, y*y_ratio, finish*x_ratio, y*y_ratio, paint);
//start = finish = last = -1;
}
overlayHolder.unlockCanvasAndPost(overlayCanvas);
}
});
}
This code generates an error sometimes when quitting the application due to some method being called after release, which is the least of my problems.
UPDATE:
Now that the orientation problem is fixed (CCD sensor orientation) I'm still facing the repetition problem, this is probably related to my YUV data management...
Your surface and camera management looks correct, but I would doublecheck that camera actually accepted preview size settings ( some camera implementations reject some settings silently)
As you are working with portrait mode, you have to keep in mind that camera does not give a fart about prhone orientation - its coordinate origin isdetermined by CCD chip and is always to right corner and scan direction is from top to bottom and right to left - quite different from your overlay canvas. ( But if you are in landscape mode, everything is correct ;) ) - this is certaily source of odd drawing result
Your threshloding is bit naive and not very usefull in real life - I would suggest adaptive threshloding. In our javaocr project ( pure java, also has android demos ) we implemented efficient sauvola binarisation (see demos):
http://sourceforge.net/projects/javaocr/
Performance binarisation can be improved to work only on single image rows (patches welcome)
Issue with UV part of image is easy - default forman is NV21, luminance comes first
and this is just byte stream, and you do not need UV part of image at all (look into demos above)