Zxing Camera Portrait Mode And Landscape On Android
Solution 1:
I used compyutech's answer, but there were some things missing. So, I'm putting everything required, here.
1. CameraConfigurationManager :
inside initFromCameraParameters
method :
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
camera.setDisplayOrientation(90);
}
also remove this code from same method :
if (width < height) {
Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
int temp = width;
width = height;
height = temp;
}
2. CameraManager :class variables
privatestaticfinalint MIN_FRAME_WIDTH = 340;
privatestaticfinalint MIN_FRAME_HEIGHT = 240;
privatestaticfinalint MAX_FRAME_WIDTH = 1200;
privatestaticfinalint MAX_FRAME_HEIGHT = 675;
getFramingRect
method :
publicsynchronized Rect getFramingRect() {
if (framingRect == null) {
if (camera == null) {
returnnull;
}
PointscreenResolution= configManager.getScreenResolution();
if (screenResolution == null) {
// Called early, before init even finishedreturnnull;
}
// Code added to enable portrait modeintwidth= MIN_FRAME_WIDTH;
intheight= MIN_FRAME_HEIGHT;
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
inttmp=7 * screenResolution.x / 8;
width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp);
tmp = 1 * screenResolution.y / 3;
height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ? MAX_FRAME_HEIGHT : (tmp));
Log.d(TAG, "Customized code for portrait mode in getFramingRect executed (Piyush Merja) ");
}else{
// Original Code
width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
}
// EndintleftOffset= (screenResolution.x - width) / 2;
inttopOffset= (screenResolution.y - height) / 2;
framingRect = newRect(leftOffset, topOffset, leftOffset + width, topOffset + height);
Log.d(TAG, "Calculated framing rect: " + framingRect);
}
return framingRect;
}
privatestaticintfindDesiredDimensionInRange(int resolution, int hardMin, int hardMax) {
intdim=5 * resolution / 8; // Target 5/8 of each dimensionif (dim < hardMin) {
return hardMin;
}
if (dim > hardMax) {
return hardMax;
}
return dim;
}
getFramingRectInPreview
method :
publicsynchronized Rect getFramingRectInPreview() {
if (framingRectInPreview == null) {
RectframingRect= getFramingRect();
if (framingRect == null) {
returnnull;
}
Rectrect=newRect(framingRect);
PointcameraResolution= configManager.getCameraResolution();
PointscreenResolution= configManager.getScreenResolution();
if (cameraResolution == null || screenResolution == null) {
// Called early, before init even finishedreturnnull;
}
// Code added to enable portrait modeif (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
Log.d(TAG, "Customized code for portrait mode in getFramingRectInPreview executed (Piyush Merja) ");
}else{
// Original code commented
rect.left = rect.left * cameraResolution.x / screenResolution.x;
rect.right = rect.right * cameraResolution.x / screenResolution.x;
rect.top = rect.top * cameraResolution.y / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
}
// End
framingRectInPreview = rect;//this was missing in compyutech's answer
}
return framingRectInPreview;
}
3. DecodeHandler :
inside decode
method :
longstart= System.currentTimeMillis();
ResultrawResult=null;
// Code added to enable portrait modeif (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
byte[] rotatedData = newbyte[data.length];
for (inty=0; y < height; y++) {
for (intx=0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
data = rotatedData;
inttmp= width;
width = height;
height = tmp;
Log.d(TAG, "Customized code for portrait mode in decode executed (Piyush Merja) ");
}//endPlanarYUVLuminanceSourcesource= activity.getCameraManager().buildLuminanceSource(data, width, height);
4. AndroidManifest.xml :
remove orientation settings for CaptureActivity
.
Solution 2:
Google this question.
I have used zxing zxing 2.3 and below solution worked for me.
1 In CameraConfigurationManager class, setDesiredCameraParameters Method add below code below pointed line
-> Camera.Parameters parameters = camera.getParameters();
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
camera.setDisplayOrientation(90);
}
2 In CameraManager class, getFramingRect Method replace code as below
int width = MIN_FRAME_WIDTH; int height = MIN_FRAME_HEIGHT;
if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
int tmp = 7 * screenResolution.x / 8;
width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp);
tmp = 1 * screenResolution.y / 3;
height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ? MAX_FRAME_HEIGHT : (tmp));
}else{
// Original Code
width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, > MAX_FRAME_WIDTH);
height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
}
3 In CameraManager class, getFramingRectInPreview Method replace code as below
if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
}else{
// Original code commented
rect.left = rect.left * cameraResolution.x / screenResolution.x;
rect.right = rect.right * cameraResolution.x / screenResolution.x;
rect.top = rect.top * cameraResolution.y / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
}
4 In DecodeHandler class, decode Method add below code below pointed line
-> Result rawResult = null;
if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
byte[] rotatedData = newbyte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
data = rotatedData;
int tmp = width;
width = height;
height = tmp;
}
Please find my working code
http://www.compyutech.co.in/repo/zxing-dynamic.zip
Hope this will help you....
Post a Comment for "Zxing Camera Portrait Mode And Landscape On Android"