i'm on creating custom camera for android, but camera previewSize is showing low resolution , how can i make that to best preview size
this is the code for i used get previewSize
private Camera.Size getBestPreviewSize(int width, int height) {
Camera.Size result = null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
return result;
}
results of this code is shows width: 352: height: 288
using below code shows some other resolution
private Camera.Size getBestPreviewSize(int width, int height)
{
List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
int targetWidth = width;
int minWidthDiff = 0;
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.width - targetWidth) < minDiff) {
if(size.width > width) {
if (minWidthDiff == 0) {
minWidthDiff = size.width - width;
optimalSize = size;
}
else if (Math.abs(size.width - targetWidth) < minWidthDiff) {
minWidthDiff = size.width - width;
optimalSize = size;
}
minDiff = Math.abs(size.width - targetWidth);
}
}
}
}
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;
}
results of this code width: 640 : height: 480
but still preview looks blur
EDIT: stop preview before using it
camera.stopPreview();
parameters.setPreviewSize(size.width, size.height);
parameters.setRotation(0);