Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update VideoStream.java #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions src/net/majorkernelpanic/streaming/video/VideoStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,133 @@ protected void unlockCamera() {
}





public void setCameraPreviewSize(int w, int h) {

List<Size> size = getSizes();
Size size_to_use = getOptimalSize(size, w, h);

System.out.println("Setting preview to: "+ w + h);

if (mCamera != null) {

if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
lockCamera();
}

Parameters parameters = mCamera.getParameters();

parameters.setPreviewSize(size_to_use.width, size_to_use.height);
//parameters.setPreviewSize(176, 144);
try {
mCamera.setParameters(parameters);
System.out.println("Setting preview to: "+ size_to_use.width + size_to_use.height + " DONE!!");
Size z = parameters.getPreviewSize();
int w_temp = z.width;
int h_temp = z.height;
System.out.println("Saved preview is: "+ w_temp + h_temp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

else {
System.out.println("Setting preview to: "+ w + h + " FAILED!!");
}



}


public synchronized List<Size> getSizes() {

List<Size> size = null;

if (mCamera != null) {

if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
lockCamera();
}


try {

Parameters parameters = mCamera.getParameters();
size = parameters.getSupportedPreviewSizes();

System.out.println("Some of the supported preview is " + size.get(0).width + "x" + size.get(0).height);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}





}

else {
System.out.println("Failed to get preview sizes");
}

return size;
}



private Size getOptimalSize(List<Size> sizes, int w, int h) {

final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes)
{

double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement

if (optimalSize == null)
{
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}



return optimalSize;
}







/**
* Computes the average frame rate at which the preview callback is called.
* We will then use this average framerate with the MediaCodec.
Expand Down