I'm playing with the API2 Camera of Google and I'm having some problems with my code. I had two different CameraSessions, one for video and another one for images. To do it more efficient I change the code to use a unique Session and make the app more efficient.
After I did this, my camera preview is not working adequately. When I'm using a 4:3 aspect ratio my preview become stretched at height. In other way it looks fine when I'm using 16:9 ratio. In both cases my pictures looks fine, I mean, preview doesn't work correctly but the pictures that I took, have the correct aspect ratio.
I already check different post with the same problem:
Camera Preview Stretched on Few Android Devices
Camera display / preview in full screen does not maintain aspect ratio - image is skewed, stretched in order to fit on the screen
But the different answers didn't help me. I know that the problem is inside my onMeasure(), setTransformMatrix() or OnLayoutChangeListener() methods, but I don't know what I'm doing wrong.
Ignore the code about Rotation, right now it's dynamic. It always enter at else condition.
Here is my code:
private OnLayoutChangeListener mLayoutListener = new OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Log.d(TAG, "[onLayoutChange] " + mCameraUI.getTextureView().getMeasuredWidth() + "x" + mCameraUI.getTextureView().getMeasuredHeight());
int width = right - left;
int height = bottom - top;
if (mPreviewWidth != width || mPreviewHeight != height
|| (mOrientationResize != mPrevOrientationResize)
|| mAspectRatioResize || mOrientationChanged) {
Log.i(TAG, "[onLayoutChange] Layout changed");
mPreviewWidth = width;
mPreviewHeight = height;
Log.i(TAG, "[onLayoutChange] Preview size: "+ mPreviewWidth + "x" + mPreviewHeight);
setTransformMatrix(width, height);
mController.onScreenSizeChanged((int) mSurfaceTextureUncroppedWidth,
(int) mSurfaceTextureUncroppedHeight);
mAspectRatioResize = false;
mOrientationChanged = true;
}
}
};
setTransform
private void setTransformMatrix(int width, int height) {
Log.i(TAG, "Screen: " + mPreviewWidth + "x" + mPreviewHeight);
mMatrix = new Matrix();
//mCameraUI.getTextureView().getTransform(mMatrix);
float scaleX = 1f, scaleY = 1f;
float scaledTextureWidth, scaledTextureHeight;
mAspectRatio= (float)height/(float)width;
if (mAspectRatio==(4f / 3f)){
scaledTextureWidth = Math.max(width,
(int) (height / mAspectRatio));
scaledTextureHeight = Math.max(height,
(int) (width * mAspectRatio));
Log.i(TAG, "[PhotoUIManager]: Aspect Ratio 4:3=" + scaledTextureWidth + "x" + scaledTextureHeight );
}
else{
scaledTextureWidth = Math.max(width,
(int) (height / mAspectRatio));
scaledTextureHeight = Math.max(height,
(int) (width * mAspectRatio));
Log.i(TAG, "[PhotoUIManager]: Aspect Ratio 16:9=" + scaledTextureWidth + "x" + scaledTextureHeight );
}
if (mSurfaceTextureUncroppedWidth != scaledTextureWidth || mSurfaceTextureUncroppedHeight != scaledTextureHeight) {
Log.e(TAG,"mi SurfaceWidth = " + mSurfaceTextureUncroppedWidth + "and mi scaledWidth=" + scaledTextureWidth);
Log.e(TAG,"mi SurfaceHeigh = " + mSurfaceTextureUncroppedHeight + "and mi scaledHeight=" + scaledTextureHeight);
mSurfaceTextureUncroppedWidth = scaledTextureWidth;
mSurfaceTextureUncroppedHeight = scaledTextureHeight;
Log.e(TAG,"Surfaces: " + mSurfaceTextureUncroppedWidth + "x" + mSurfaceTextureUncroppedHeight);
if (mSurfaceTextureSizeListener != null) {
mSurfaceTextureSizeListener.onSurfaceTextureSizeChanged(
(int) mSurfaceTextureUncroppedWidth, (int) mSurfaceTextureUncroppedHeight);
}
}
scaleX = scaledTextureWidth / width;
scaleY = scaledTextureHeight / height;
mMatrix.setScale(scaleX, scaleY, scaledTextureWidth/2, scaledTextureHeight/2);
Log.e(TAG, "scale: X= " + scaleX + " Y=" + scaleY + "Width= " + scaledTextureWidth + "Height= " + scaledTextureHeight);
// init the position (this seems to be necessary too when the ratio is 16/9
mCameraUI.getTextureView().setX(0);
mCameraUI.getTextureView().setY(0);
// Translate the preview with the rotation is aspect ration is 4/3
if (mAspectRatio == 4f / 3f) {
Log.e(TAG, "aspect ratio standard");
float verticalTranslateOffset = (mCameraUI.getTextureView().getMeasuredHeight() - scaledTextureHeight) / 2;
float horizontalTranslateOffset = (mCameraUI.getTextureView().getMeasuredWidth() - scaledTextureWidth) / 2;
int rotation = CameraUtil.getDisplayRotation(mActivity);
switch (rotation) {
case 0:
// phone portrait; translate the preview up
mCameraUI.getTextureView().setY(-verticalTranslateOffset);
mFaceView.setStandardPreviewTranslationOffset(-verticalTranslateOffset);
mFocusView.setStandardPreviewTranslationOffset(-verticalTranslateOffset);
break;
case 90:
// phone landscape: translate the preview left
mCameraUI.getTextureView().setX(-horizontalTranslateOffset);
mFaceView.setStandardPreviewTranslationOffset(-horizontalTranslateOffset);
mFocusView.setStandardPreviewTranslationOffset(-horizontalTranslateOffset);
break;
case 180:
// phone upside down: translate the preview bottom
mCameraUI.getTextureView().setY(verticalTranslateOffset);
mFaceView.setStandardPreviewTranslationOffset(verticalTranslateOffset);
mFocusView.setStandardPreviewTranslationOffset(verticalTranslateOffset);
break;
case 270:
// reverse landscape: translate the preview right
mCameraUI.getTextureView().setX(horizontalTranslateOffset);
mFaceView.setStandardPreviewTranslationOffset(horizontalTranslateOffset);
mFocusView.setStandardPreviewTranslationOffset(horizontalTranslateOffset);
break;
}
} else {
Log.e(TAG, "aspect ratio full");
mFaceView.setStandardPreviewTranslationOffset(0);
mFocusView.setStandardPreviewTranslationOffset(0);
}
mRenderOverlay.updateLayout();
mCameraUI.getTextureView().setTransform(mMatrix);
RectF previewRect = new RectF(0, 0, width, height);
mController.onPreviewRectChanged(CameraUtil.rectFToRect(previewRect));
}
onMeasure
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
Log.d(TAG, "onMeasure PREVIOUS. Width x Height [" + widthMeasureSpec + " = " + width + "x" + heightMeasureSpec + " = " + height + "]");
int rotation = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRotation();
boolean isInHorizontal = Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation;
int newWidth;
int newHeight;
if (isInHorizontal) {
newHeight = getMeasuredHeight();
newWidth = (int) (newHeight * mAspectRatio);
} else {
newWidth = getMeasuredWidth();
newHeight = (int) (newWidth * mAspectRatio);
}
setMeasuredDimension(newWidth, newHeight);
Log.d(TAG, "onMeasure. Width x Height [" + newWidth + "x" + newHeight + "]");
}
Solved!
I had a default values for the BufferSize of my texture, which just are restarted when I inicializated a new Session or after change the after ratio. But the Width and Height values for the texture were not updated with the ratio, so it becomes streched again and again.
I solved it changing my defaultbufferSize with the PreviewSizes which I update always that i change the ratio.
public void createCameraPreviewSession(Size previewsize, Surface recordingSurface) {
try {
if (mCaptureSession != null) {
mCaptureSession.stopRepeating();
mCaptureSession.close();
mCaptureSession = null;
}
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
List<Surface> surfaces = new ArrayList<Surface>();
// We configure the size of default buffer to be the size of camera preview we want.
texture.setDefaultBufferSize(previewsize.getWidth(),previewsize.getHeight());
;
// This is the output Surface we need to start preview.
Surface surface = new Surface(texture);
.......
Related
Im trying to use camera 2 with and AutoFitTextureView from the google sample.
Im setting it up like this
private void setUpCameraOutputs() {
CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
try {
if (manager != null) {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
// We don't use a front facing camera in this sample.
//noinspection ConstantConditions
if (characteristics.get(LENS_FACING) == LENS_FACING_FRONT) continue;
StreamConfigurationMap map = characteristics.get(SCALER_STREAM_CONFIGURATION_MAP);
if(map != null) {
// For still image captures, we use the largest available size.
List<Size> outputSizes = Arrays.asList(map.getOutputSizes(sImageFormat));
Size largest = Collections.max(outputSizes, new CompareSizesByArea());
mImageReader = ImageReader.newInstance(640, 480, sImageFormat, 8);
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
// Danger, W.R.! Attempting to use too large a preview size could exceed the camera
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of
// garbage capture data.
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), largest.getWidth(), largest.getHeight(), largest);
//mPreviewSize = new Size(largest.getWidth(), largest.getHeight());
setAspectRatio2(mPreviewSize);
Logging.e(TAG, "WIDTH: " + mPreviewSize.getWidth() + " HEIGHT: " + mPreviewSize.getHeight());
mCameraId = cameraId;
}
//return;
}
}else{
Logging.e(TAG,"No Manager");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setAspectRatio(){
int orientation = mContext.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTextureView.setAspectRatio(mPreviewSize.getWidth(),mPreviewSize.getHeight());
} else {
mTextureView.setAspectRatio(mPreviewSize.getHeight(),mPreviewSize.getWidth());
}
}
private void setAspectRatio2(Size largest){
float cameraAspectRatio = (float) largest.getHeight() / largest.getWidth();
//Preparation
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE));
wm.getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int finalWidth = screenWidth;
int finalHeight = screenHeight;
int widthDifference = 0;
int heightDifference = 0;
float screenAspectRatio = (float) screenWidth / screenHeight;
//Determines whether we crop width or crop height
if (screenAspectRatio > cameraAspectRatio) { //Keep width crop height
finalHeight = (int) (screenWidth / cameraAspectRatio);
heightDifference = finalHeight - screenHeight;
} else { //Keep height crop width
finalWidth = (int) (screenHeight * cameraAspectRatio);
widthDifference = finalWidth - screenWidth;
}
//Apply the result to the Preview
LayoutParams lp = (FrameLayout.LayoutParams) mTextureView.getLayoutParams();
lp.width = finalWidth;
lp.height = finalHeight;
//Below 2 lines are to center the preview, since cropping default occurs at the right and bottom
lp.leftMargin = - (widthDifference / 2);
lp.topMargin = - (heightDifference / 2);
mTextureView.setLayoutParams(lp);
}
private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
List<Size> bigEnough = new ArrayList<>();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
double ratio = (double) h / w;
for (Size option : choices) {
double optionRatio = (double) option.getHeight() / option.getWidth();
if (ratio == optionRatio) {
bigEnough.add(option);
}
}
// Pick the smallest of those, assuming we found any
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else {
Logging.e(TAG, "Couldn't find any suitable preview size");
return choices[1];
}
}
I've tried to set the aspect ratio with 2 different methods, but none of them seem to do what i need.
The preview takes all screen as i want, but camera preview always appear streeched, things look thiner.
I've tried a few sugestions from stackoverflow but still haven't got the perfect result.
There are a lot of libraries which ease the use of CameraView.
You can have a look at this one or this one for example.
I have a TextureView in which a video is showing, a video previously recorded with device camera. The image in the video is stretched, and that is not acceptable. I did some research, and found this method:
private void adjustAspectRatio(int videoWidth, int videoHeight) {
int viewWidth = m_TextureView.getWidth();
int viewHeight = m_TextureView.getHeight();
double aspectRatio = (double) videoHeight / videoWidth;
int newWidth, newHeight;
if (viewHeight > (int) (viewWidth * aspectRatio)) {
// limited by narrow width; restrict height
newWidth = viewWidth;
newHeight = (int) (viewWidth * aspectRatio);
} else {
// limited by short height; restrict width
newWidth = (int) (viewHeight / aspectRatio);
newHeight = viewHeight;
}
int xoff = (viewWidth - newWidth) / 2;
int yoff = (viewHeight - newHeight) / 2;
Log.v(TAG, "video=" + videoWidth + "x" + videoHeight +
" view=" + viewWidth + "x" + viewHeight +
" newView=" + newWidth + "x" + newHeight +
" off=" + xoff + "," + yoff);
Matrix txform = new Matrix();
m_TextureView.getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
//txform.postRotate(10); // just for fun
txform.postTranslate(xoff, yoff);
m_TextureView.setTransform(txform);
}
Tried to use it, but I see a black background only.
This is mu onSurfaceTextureAvailable, in case it helps:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
isVideoResultReady=true;
Surface s=new Surface(surface);
if(mPlayer==null){
mPlayer=new MediaPlayer();
}
try {
//mPlayer.setDataSource(videoPath);
mPlayer.setSurface(s);
mPlayer.prepare();
mPlayer.setOnBufferingUpdateListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnPreparedListener(this);
//media player is started in other point of the app
} catch (IOException e) {
e.printStackTrace();
}
}
What is wrong with that method? How to keep aspect ratio? Thanks.
I'm working with camera2 and I'm showing a preview of my photo/video after longclick in my thumbnail. Also, I'm rotating it depending of which orientation the camera had when the picture was taken. For example, if I did a picture in 90º, my preview will be also rotated 90º.
Everything is working fine, I'm using a customContainer and there I'm using onLayout and OnMeasure to create my preview depending of the size of the screen, aspect ratio and orientation. It works fine with photos. My problem appear when I try to do the same with videos, they only work in 0º.
I tried to rotate the TextureView which contain my MediaPlayer but after this my onLayout become crazy and Itś impossible find a (l,t,r,b) combination to measure it correctly.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<com.android.camera.ui.common.ThumbnailContainer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/preview_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rounded_rectangle_thumbnail_preview"
android:visibility="invisible">
<TextureView
android:id="#+id/show_video_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
<ImageView
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:visibility="invisible"
/>
</com.android.camera.ui.common.ThumbnailContainer>
Here is my Surface code:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.i(TAG, "InicializoSurface. Width: " + width + " HEIGHT:" + height);
Log.i(TAG, "InicializoSurface. Width: " + mVideoView.getMeasuredWidth() + " HEIGHT:" + mVideoView.getMeasuredHeight());
Log.i(TAG, "View transform. Width: " + mVideoView.getWidth() + " HEIGHT:" + mVideoView.getHeight());
mMediaSurface = new Surface(mVideoView.getSurfaceTexture());
initializeMediaPlayer();
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (mMediaPlayer != null) {
// Make sure we stop video and release resources when activity is destroyed.
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
//////////
private void initializeMediaPlayer(){
mMediaPlayer = new CustomMediaPlayer();
Uri uri = Uri.parse(mCameraDataAdapter.getList().get(0).getPath());
try {
mMediaPlayer.setDataSource(mActivity, uri);
mMediaPlayer.setSurface(mMediaSurface);
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(mMediaPlayer);
mMediaPlayer.setOnCompletionListener(mMediaPlayer);
} catch (IOException e) {
e.printStackTrace();
}
}
///////////
mVideoView.setVisibility(View.VISIBLE);
// mVideoView.setTranslationX(-200);
// mVideoView.setTranslationY(-200);
Log.i(TAG, "X: " + mVideoView.getX() + "Y: " + mVideoView.getY());
if (mVideoView.isAvailable()) {
onSurfaceTextureAvailable(mVideoView.getSurfaceTexture(), mVideoView.getWidth(), mVideoView.getHeight());
}
if (mMediaPlayer == null) {
initializeMediaPlayer();
}
// mMediaPlayer.mVideoHolder = mVideoView.getHolder();
// mMediaPlayer.setDisplay(mMediaPlayer.mVideoHolder);
if (mMediaPrepared) {
Log.i(TAG,"Comienzo Video");
mMediaPlayer.start();
}
Finally here is my onMeasure/OnLayout from my CustomView
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width;
int height;
int wantedWidth = 0;
int wantedHeight = 0;
if(mWidth == 0 && mHeight == 0 ){
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight =MeasureSpec.getSize(heightMeasureSpec);
}
width = mWidth;
height = mHeight;
if (mOrientation == 0 || mOrientation == 180) {
wantedWidth = width - (int)(mMargin * 2);
mVideo.measure(MeasureSpec.makeMeasureSpec(wantedWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) (wantedWidth * mVideoAspectRatio), MeasureSpec.EXACTLY));
wantedHeight = (mViewTop.getLayoutParams().height) * 2 + (int) (wantedWidth * mAspectRatio);
} else {
Log.e(TAG, "Real Width = " + width + " real Height = " + height);
wantedHeight = width - 2 * mViewTop.getLayoutParams().height - (int)(mMargin * 2);
mVideo.measure(MeasureSpec.makeMeasureSpec(wantedHeight, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec((int) (wantedHeight * mAspectRatio), MeasureSpec.EXACTLY));
//
wantedWidth =(int) (wantedHeight * mAspectRatio) ;
wantedHeight = width - (int)(mMargin * 2);
}
Log.e(TAG, "onMeasure: " + wantedWidth + "x" + wantedHeight);
setMeasuredDimension(MeasureSpec.makeMeasureSpec(wantedWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(wantedHeight, MeasureSpec.EXACTLY));
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int w = getMeasuredWidth();
int h = getMeasuredHeight();
int viewHeight = mViewBottom.getMeasuredHeight();
int imageViewHeight = mImage.getMeasuredHeight();
int wantedHeight = 0;
// w = w - (int) (2 * mMargin);
if (mOrientation == 0 || mOrientation == 180) {
mVideo.layout(0,wantedHeight,w,wantedHeight + imageViewHeight);
}else{
mVideo.layout(viewHeight,0,r-viewHeight - (int) mMargin,w);
}
}
I have been looking in other post as Android MediaRecorder making rotated video and I saw that it's not possible to rotate the textureView, but I can't believe that I can rotate a image so easily and have to fight during this to rotate 90 degrees a video.
Thanks to #pskink for their comments in the post I found a solution with him. Finally I used a Matrix to rotate the Video Container(Texture View). The method that pskink give me is the next one:
private void setupMatrix(int width, int height, int degrees, boolean isHorizontal) {
Log.d(TAG, "setupMatrix for " + degrees + " degrees");
Matrix matrix = new Matrix();
//The video will be streched if the aspect ratio is in 1,5(recording at 480)
RectF src;
if (isHorizontal)
//In my case, I changed this line, because with my onMeasure() and onLayout() methods my container view is already rotated and scaled, so I need to sent the inverted params to the src.
src = new RectF(0, 0,mThumbnailContainer.getmWidth(), mThumbnailContainer.getmHeight());
else
src = new RectF(0, 0, mThumbnailContainer.getmWidth(),mThumbnailContainer.getmHeight());
RectF dst = new RectF(0, 0, width, height);
RectF screen = new RectF(dst);
Log.d(TAG, "Matrix: " + width + "x" + height);
Log.d(TAG, "Matrix: " + mThumbnailContainer.getmWidth() + "x" + mThumbnailContainer.getmHeight());
matrix.postRotate(degrees, screen.centerX(), screen.centerY());
matrix.mapRect(dst);
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
matrix.mapRect(src);
matrix.setRectToRect(screen, src, Matrix.ScaleToFit.FILL);
matrix.postRotate(degrees, screen.centerX(), screen.centerY());
mVideoView.setTransform(matrix);
}
Finally it worked and it looks totally awesome. With this I have been able to rotate and scale any video totally dynamically depending of the screen of my device and the Aspect Ratio used for record the video or take the picture.
I use a Frame Layout, on which i bind my video player like this:
private void initVideoPlayer(View root) {
mVideoPlayer = new TextureViewVideoPlayer(width, height);
mVideoPlayer.setOnErrorListener(this);
mVideoPlayer.setOnPreparedListener(this);
mVideoPlayer.setOnCompletionListener(this);
mVideoPlayer.setOnBufferingUpdateListener(this);
mVideoPlayer.setOnSeekCompleteListener(this);
mVideoPlayer.setOnVideoSizeChangedListener(this);
mVideoPlayer.bindView((FrameLayout) root.findViewById(R.id.videoFrame), 0);
}
This is the onVideoSizeChanged function, from the TextureViewVideoPlayer:
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.i(TAG, "onVideoSizeChanged " + width + " ZZ " + height + " Display Height: " + displayheight + " --- REAL VIDEO RATIO: -- " + ((double) width / (double) height));
if (this.mOnVideoSizeChangedListener != null)
this.mOnVideoSizeChangedListener.onVideoSizeChanged(this, width, height);
if (width == 0 || height == 0) {
mp.release();
Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
return;
}
double ratio = ((double) width / (double) height);
if (displayheight > height) {
ratio = ((double) displayheight / (double) height);
} else {
ratio = ((double) height / (double) displayheight);
}
LayoutParams params = new FrameLayout.LayoutParams((int) (width * ratio), (int) (height * ratio));
// LayoutParams params = new FrameLayout.LayoutParams((width),
// (height));
Log.i(TAG, "onVideoSizeChanged mod" + params.width + " ZZ " + params.height + " RATIO: " + ratio + " --- REAL RATIO: -- " + ((double) params.width / (double) params.height));
params.gravity = Gravity.CENTER;
mTextureView.setLayoutParams(params);
this.mTextureView.requestLayout();
}
As you can see, it takes the video width and height of the video, it calculates the ratio, then it takes the displays height, and makes a ratio to see how much bigger (or smaller) the video is than the screen, then it creates the params for the video, with that ratio, and sets this parameters on the textureView.
In the xml, the VideoFrame i bind my player too has this:
<FrameLayout
android:id="#+id/videoFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
</FrameLayout>
Now, if I keep it like this, the video is stretched, and even though it looks like its a rectangle, a part from the top and bottom of the video disappear (being 1:1 the width and height are the same, and the height is bigger that the screen). Now, if I don't use the ratio, and create the parameters with the width and height of the video (640 x 480) It will be a small square. (ratio 1:1), and stretched. Any ideea how this could be avoided?
PS: My texture view in fact is a Square Texture View, which had this onMeasure function:
#Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
setMeasuredDimension(i, i);
}
I've changed it into:
#Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
int j = View.MeasureSpec.getSize(paramInt2);
setMeasuredDimension(i, j);
}
Hopefully, this fixes my issues.
MY texture view had this:
#Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
setMeasuredDimension(i, i);
}
To fix it, I've changed it into:
#Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
int j = View.MeasureSpec.getSize(paramInt2);
setMeasuredDimension(i, j);
}
I want to show portrait orientation on Zxing's camera.
How can this be done?
Here's how it works.
Step 1: Add following lines to rotate data before buildLuminanceSource(..) in decode(byte[] data, int width, int height)
DecodeHandler.java:
byte[] rotatedData = new byte[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];
}
int tmp = width;
width = height;
height = tmp;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
Step 2: Modify getFramingRectInPreview().
CameraManager.java
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;
Step 3: Disable the check for Landscape Mode in initFromCameraParameters(...)
CameraConfigurationManager.java
//remove the following
if (width < height) {
Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
int temp = width;
width = height;
height = temp;
}
Step 4: Add following line to rotate camera insetDesiredCameraParameters(...)
CameraConfigurationManager.java
camera.setDisplayOrientation(90);
Step 5: Do not forget to set orientation of activity to portrait. I.e: manifest
To support all orientation and change automatically when rotating the activity do this, all you have to modify is the CameraManager.javaclass.
And remove this method getCurrentOrientation() from CaptureActivity.java
In CameraManager.java Create this variable:
int resultOrientation;
Add this to the openDriver(..) method:
setCameraDisplayOrientation(context, Camera.CameraInfo.CAMERA_FACING_BACK, theCamera);//this can be set after camera.setPreviewDisplay(); in api13+.
****Create this method****
Link: http://developer.android.com/reference/android/hardware/Camera.html
public static void setCameraDisplayOrientation(Context context,int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int degrees = 0;
switch (display.getRotation()) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
resultOrientation = (info.orientation + degrees) % 360;
resultOrientation = (360 - resultOrientation) % 360; // compensate the mirror
} else { // back-facing
resultOrientation = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(resultOrientation);
}
****Now modify getFramingRectInPreview()****
if(resultOrientation == 180 || resultOrientation == 0){//to work with landScape and reverse landScape
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;
}else{
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;
}
And modify this method public PlanarYUVLuminanceSource buildLuminanceSource(..)
if(resultOrientation == 180 || resultOrientation == 0){//TODO: This is to use camera in landScape mode
// Go ahead and assume it's YUV rather than die.
return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
}else{
byte[] rotatedData = new byte[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];
}
int tmp = width;
width = height;
height = tmp;
return new PlanarYUVLuminanceSource(rotatedData, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
}
You can use my fork of zxlib https://github.com/rusfearuth/zxing-lib-without-landscape-only. I disabled landscape mode only. You can set landscape/portrait and see correct camera view.
Adding camera.setDisplayOrientation(90); in CameraConfigurationManager.java worked for me.
for zxing 3.0, working lib https://github.com/xiaowei4895/zxing-android-portrait
for portrait mode
Thank you
I think the best library only solution is this one ...
https://github.com/SudarAbisheck/ZXing-Orient
You can include it in build.gradle as a dependency of your project in maven format ...
dependencies {
compile ''me.sudar:zxing-orient:2.1.1#aar''
}
Create AnyOrientationCaptureActivity and then override default CaptureActivity then it will work.
public void scanCode() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(CommonUtil.POSEIDON_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.setOrientationLocked(false);
//Override here
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
integrator.initiateScan();
}
//create AnyOrientationCaptureActivity extend CaptureActivity
public class AnyOrientationCaptureActivity extends CaptureActivity {
}
Define in manifest
<activity
android:name=".views.AnyOrientationCaptureActivity"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:theme="#style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
This is supposed to be a synched version to the above solution
https://github.com/zxing/zxing/tree/4b124b109d90ac2960078ce68e15a39885fc1b5b
Additionally to #roylee's modification I had to apply the following to the CameraConfigurationManager.java in order to get best possible preview and QR code recognition quality
diff --git a/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java b/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
index cd9d0d8..4f12c8c 100644
--- a/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
+++ b/android/src/com/google/zxing/client/android/camera/CameraConfigurationManager.java
## -56,21 +56,24 ## public final class CameraConfigurationManager {
Display display = manager.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
- // We're landscape-only, and have apparently seen issues with display thinking it's portrait
+ // We're landscape-only, and have apparently seen issues with display thinking it's portrait
// when waking from sleep. If it's not landscape, assume it's mistaken and reverse them:
+ /*
if (width < height) {
Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
int temp = width;
width = height;
height = temp;
}
+ */
screenResolution = new Point(width, height);
Log.i(TAG, "Screen resolution: " + screenResolution);
- cameraResolution = findBestPreviewSizeValue(parameters, screenResolution, false);
+ cameraResolution = findBestPreviewSizeValue(parameters, screenResolution, true);//
Log.i(TAG, "Camera resolution: " + cameraResolution);
}
void setDesiredCameraParameters(Camera camera) {
+ camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
if (parameters == null) {
## -99,7 +102,7 ## public final class CameraConfigurationManager {
Point getScreenResolution() {
return screenResolution;
}
-
+
public void setFrontCamera(boolean newSetting) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean currentSetting = prefs.getBoolean(PreferencesActivity.KEY_FRONT_CAMERA, false);
## -109,12 +112,12 ## public final class CameraConfigurationManager {
editor.commit();
}
}
-
+
public boolean getFrontCamera() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean(PreferencesActivity.KEY_FRONT_CAMERA, false);
}
-
+
public boolean getTorch() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false);
## -181,7 +184,14 ## public final class CameraConfigurationManager {
Camera.Size defaultSize = parameters.getPreviewSize();
bestSize = new Point(defaultSize.width, defaultSize.height);
}
+
+ // FIXME: test the bestSize == null case!
+ // swap width and height in portrait case back again
+ if (portrait) {
+ bestSize = new Point(bestSize.y, bestSize.x);
+ }
return bestSize;
+
}
private static String findSettableValue(Collection<String> supportedValues,