根据Camera支持分辨率选择最合适的分辨率

根据Camera支持分辨率选择最合适的分辨率
pull/107/head
xufulong 5 years ago
parent fec6195e98
commit 81b7ee58d0
  1. 10
      Live/src/main/java/com/frank/live/Push/VideoPusherNew.java
  2. 109
      Live/src/main/java/com/frank/live/camera2/Camera2Helper.java

@ -34,8 +34,8 @@ public class VideoPusherNew extends Pusher implements TextureView.SurfaceTexture
this.mLiveUtil = liveUtil; this.mLiveUtil = liveUtil;
this.mContext = context; this.mContext = context;
mTextureView.setSurfaceTextureListener(this); mTextureView.setSurfaceTextureListener(this);
liveUtil.setVideoParams(videoParam.getWidth(), videoParam.getHeight(), // liveUtil.setVideoParams(videoParam.getWidth(), videoParam.getHeight(),
videoParam.getBitRate(), videoParam.getFrameRate()); // videoParam.getBitRate(), videoParam.getFrameRate());
} }
@ -66,8 +66,6 @@ public class VideoPusherNew extends Pusher implements TextureView.SurfaceTexture
Log.e(TAG, "preview width=" + mTextureView.getWidth() + "--height=" + mTextureView.getHeight()); Log.e(TAG, "preview width=" + mTextureView.getWidth() + "--height=" + mTextureView.getHeight());
camera2Helper = new Camera2Helper.Builder() camera2Helper = new Camera2Helper.Builder()
.cameraListener(this) .cameraListener(this)
.maxPreviewSize(new Point(1080, 720))
.minPreviewSize(new Point(mVideoParam.getWidth(), mVideoParam.getHeight()))
.specificCameraId(Camera2Helper.CAMERA_ID_BACK) .specificCameraId(Camera2Helper.CAMERA_ID_BACK)
.context(mContext.getApplicationContext()) .context(mContext.getApplicationContext())
.previewOn(mTextureView) .previewOn(mTextureView)
@ -140,6 +138,10 @@ public class VideoPusherNew extends Pusher implements TextureView.SurfaceTexture
@Override @Override
public void onCameraOpened(Size previewSize, int displayOrientation) { public void onCameraOpened(Size previewSize, int displayOrientation) {
Log.e(TAG, "onCameraOpened previewSize=" + previewSize.toString()); Log.e(TAG, "onCameraOpened previewSize=" + previewSize.toString());
if (mLiveUtil != null && mVideoParam != null) {
mLiveUtil.setVideoParams(previewSize.getWidth(), previewSize.getHeight(),
mVideoParam.getBitRate(), mVideoParam.getFrameRate());
}
} }
@Override @Override

@ -28,7 +28,7 @@ import android.view.TextureView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -45,9 +45,6 @@ public class Camera2Helper {
private static final String TAG = Camera2Helper.class.getSimpleName(); private static final String TAG = Camera2Helper.class.getSimpleName();
private Point maxPreviewSize;
private Point minPreviewSize;
public static final String CAMERA_ID_FRONT = "1"; public static final String CAMERA_ID_FRONT = "1";
public static final String CAMERA_ID_BACK = "0"; public static final String CAMERA_ID_BACK = "0";
@ -58,7 +55,6 @@ public class Camera2Helper {
private TextureView mTextureView; private TextureView mTextureView;
private int rotation; private int rotation;
private Point previewViewSize; private Point previewViewSize;
private Point specificPreviewSize;
private Context context; private Context context;
/** /**
* A {@link CameraCaptureSession } for camera preview. * A {@link CameraCaptureSession } for camera preview.
@ -78,9 +74,6 @@ public class Camera2Helper {
camera2Listener = builder.camera2Listener; camera2Listener = builder.camera2Listener;
rotation = builder.rotation; rotation = builder.rotation;
previewViewSize = builder.previewViewSize; previewViewSize = builder.previewViewSize;
specificPreviewSize = builder.previewSize;
maxPreviewSize = builder.maxPreviewSize;
minPreviewSize = builder.minPreviewSize;
context = builder.context; context = builder.context;
} }
@ -250,63 +243,21 @@ public class Camera2Helper {
private Size getBestSupportedSize(List<Size> sizes) { private Size getBestSupportedSize(List<Size> sizes) {
Size defaultSize = sizes.get(0); Size defaultSize = sizes.get(0);
Size[] tempSizes = sizes.toArray(new Size[0]); Log.e(TAG, "default width=" + defaultSize.getWidth() + "--height=" + defaultSize.getHeight());
Arrays.sort(tempSizes, new Comparator<Size>() { int defaultDelta = Math.abs(defaultSize.getWidth() * defaultSize.getHeight() - previewViewSize.x * previewViewSize.y);
@Override Iterator supportIterator = sizes.iterator();
public int compare(Size o1, Size o2) { while (supportIterator.hasNext()) {
if (o1.getWidth() > o2.getWidth()) { Size size = (Size) supportIterator.next();
return -1; Log.e(TAG, "current width=" + defaultSize.getWidth() + "--height=" + defaultSize.getHeight());
} else if (o1.getWidth() == o2.getWidth()) { int currentDelta = Math.abs(size.getWidth() * size.getHeight() - previewViewSize.x * previewViewSize.y);
return o1.getHeight() > o2.getHeight() ? -1 : 1; if (currentDelta < defaultDelta) {
} else { defaultDelta = currentDelta;
return 1; defaultSize = size;
} }
} }
}); Log.e(TAG, "final width=" + defaultSize.getWidth() + "--height=" + defaultSize.getHeight());
sizes = new ArrayList<>(Arrays.asList(tempSizes));
for (int i = sizes.size() - 1; i >= 0; i--) {
if (maxPreviewSize != null) {
if (sizes.get(i).getWidth() > maxPreviewSize.x || sizes.get(i).getHeight() > maxPreviewSize.y) {
sizes.remove(i);
continue;
}
}
if (minPreviewSize != null) {
if (sizes.get(i).getWidth() < minPreviewSize.x || sizes.get(i).getHeight() < minPreviewSize.y) {
sizes.remove(i);
}
}
}
if (sizes.size() == 0) {
String msg = "can not find suitable previewSize, now using default";
if (camera2Listener != null) {
Log.e(TAG, msg);
camera2Listener.onCameraError(new Exception(msg));
}
return defaultSize; return defaultSize;
} }
Size bestSize = sizes.get(0);
float previewViewRatio;
if (previewViewSize != null) {
previewViewRatio = (float) previewViewSize.x / (float) previewViewSize.y;
} else {
previewViewRatio = (float) bestSize.getWidth() / (float) bestSize.getHeight();
}
if (previewViewRatio > 1) {
previewViewRatio = 1 / previewViewRatio;
}
for (Size s : sizes) {
if (specificPreviewSize != null && specificPreviewSize.x == s.getWidth() && specificPreviewSize.y == s.getHeight()) {
return s;
}
if (Math.abs((s.getHeight() / (float) s.getWidth()) - previewViewRatio) < Math.abs(bestSize.getHeight() / (float) bestSize.getWidth() - previewViewRatio)) {
bestSize = s;
}
}
return bestSize;
}
public synchronized void start() { public synchronized void start() {
if (mCameraDevice != null) { if (mCameraDevice != null) {
@ -396,11 +347,7 @@ public class Camera2Helper {
} }
cameraManager.openCamera(mCameraId, mDeviceStateCallback, mBackgroundHandler); cameraManager.openCamera(mCameraId, mDeviceStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) { } catch (CameraAccessException | InterruptedException e) {
if (camera2Listener != null) {
camera2Listener.onCameraError(e);
}
} catch (InterruptedException e) {
if (camera2Listener != null) { if (camera2Listener != null) {
camera2Listener.onCameraError(e); camera2Listener.onCameraError(e);
} }
@ -537,12 +484,6 @@ public class Camera2Helper {
private int rotation; private int rotation;
private Point previewSize;
private Point maxPreviewSize;
private Point minPreviewSize;
private Context context; private Context context;
public Builder() { public Builder() {
@ -554,21 +495,6 @@ public class Camera2Helper {
return this; return this;
} }
public Builder previewSize(Point val) {
previewSize = val;
return this;
}
public Builder maxPreviewSize(Point val) {
maxPreviewSize = val;
return this;
}
public Builder minPreviewSize(Point val) {
minPreviewSize = val;
return this;
}
public Builder previewViewSize(Point val) { public Builder previewViewSize(Point val) {
previewViewSize = val; previewViewSize = val;
return this; return this;
@ -599,11 +525,6 @@ public class Camera2Helper {
if (previewDisplayView == null) { if (previewDisplayView == null) {
throw new NullPointerException("must preview on a textureView or a surfaceView"); throw new NullPointerException("must preview on a textureView or a surfaceView");
} }
if (maxPreviewSize != null && minPreviewSize != null) {
if (maxPreviewSize.x < minPreviewSize.x || maxPreviewSize.y < minPreviewSize.y) {
throw new IllegalArgumentException("maxPreviewSize must greater than minPreviewSize");
}
}
return new Camera2Helper(this); return new Camera2Helper(this);
} }
} }

Loading…
Cancel
Save