You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.0 KiB

package com.yeyupiaoling.teststadiometry;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Size;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Utils {
private static final String TAG = Utils.class.getName();
// 获取最优的预览图片大小
public static Size chooseOptimalSize(final Size[] choices, final int width, final int height) {
final Size desiredSize = new Size(width, height);
// Collect the supported resolutions that are at least as big as the preview Surface
boolean exactSizeFound = false;
float desiredAspectRatio = width * 1.0f / height; //in landscape perspective
float bestAspectRatio = 0;
final List<Size> bigEnough = new ArrayList<Size>();
for (final Size option : choices) {
if (option.equals(desiredSize)) {
// Set the size but don't return yet so that remaining sizes will still be logged.
exactSizeFound = true;
break;
}
float aspectRatio = option.getWidth() * 1.0f / option.getHeight();
if (aspectRatio > desiredAspectRatio) continue; //smaller than screen
//try to find the best aspect ratio which fits in screen
if (aspectRatio > bestAspectRatio) {
if (option.getHeight() >= height && option.getWidth() >= width) {
bigEnough.clear();
bigEnough.add(option);
bestAspectRatio = aspectRatio;
}
} else if (aspectRatio == bestAspectRatio) {
if (option.getHeight() >= height && option.getWidth() >= width) {
bigEnough.add(option);
}
}
}
if (exactSizeFound) {
return desiredSize;
}
if (bigEnough.size() > 0) {
final Size chosenSize = Collections.min(bigEnough, new Comparator<Size>() {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum(
(long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight());
}
});
return chosenSize;
} else {
return choices[0];
}
}
public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
public static List<Bitmap> bisectionBitmap(Bitmap bitmap) {
List<Bitmap> bitmapList = new ArrayList<>();
Bitmap left = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() / 2, bitmap.getHeight(), null, true);
bitmapList.add(left);
Bitmap right = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 2, 0, bitmap.getWidth() / 2, bitmap.getHeight(), null, true);
bitmapList.add(right);
return bitmapList;
}
}