|
|
|
@ -1,11 +1,14 @@
|
|
|
|
|
package org.easydarwin.util;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Matrix;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.text.TextPaint;
|
|
|
|
|
|
|
|
|
|
public class YUVUtils {
|
|
|
|
|
//缩小图片到制定长宽
|
|
|
|
|
public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight) {
|
|
|
|
|
public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight, int cameraRotationOffset) {
|
|
|
|
|
if (bm == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -14,6 +17,7 @@ public class YUVUtils {
|
|
|
|
|
float scaleWidth = ((float) newWidth) / width;
|
|
|
|
|
float scaleHeight = ((float) newHeight) / height;
|
|
|
|
|
Matrix matrix = new Matrix();
|
|
|
|
|
matrix.postRotate(360 - cameraRotationOffset);
|
|
|
|
|
matrix.postScale(scaleWidth, scaleHeight);
|
|
|
|
|
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
|
|
|
|
|
true);
|
|
|
|
@ -29,8 +33,9 @@ public class YUVUtils {
|
|
|
|
|
if (bitmap == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
int width = bitmap.getWidth();
|
|
|
|
|
int height = bitmap.getHeight();
|
|
|
|
|
// /2*2保证都是偶数
|
|
|
|
|
int width = bitmap.getWidth() / 2 * 2;
|
|
|
|
|
int height = bitmap.getHeight() / 2 * 2;
|
|
|
|
|
|
|
|
|
|
int size = width * height;
|
|
|
|
|
|
|
|
|
@ -70,4 +75,33 @@ public class YUVUtils {
|
|
|
|
|
}
|
|
|
|
|
return yuv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Bitmap generateBitmap(String text, int textSizePx, int textColor) {
|
|
|
|
|
TextPaint textPaint = new TextPaint();
|
|
|
|
|
textPaint.setTextSize(textSizePx);
|
|
|
|
|
textPaint.setColor(textColor);
|
|
|
|
|
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
|
|
|
|
|
int lineHeight = (int) Math.ceil(Math.abs(fontMetrics.bottom) + Math.abs(fontMetrics.top));
|
|
|
|
|
Bitmap bitmap = null;
|
|
|
|
|
if (text.contains("@")) {
|
|
|
|
|
String[] split = text.split("@");
|
|
|
|
|
int width = 0;
|
|
|
|
|
for (int i = 0; i < split.length; i++) {
|
|
|
|
|
width = Math.max(width, (int) Math.ceil(textPaint.measureText(split[i])));
|
|
|
|
|
}
|
|
|
|
|
int height = lineHeight * split.length;
|
|
|
|
|
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
|
for (int i = 0; i < split.length; i++) {
|
|
|
|
|
canvas.drawText(split[i], 0, Math.abs(fontMetrics.ascent) + lineHeight * i, textPaint);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int width = (int) Math.ceil(textPaint.measureText(text));
|
|
|
|
|
int height = lineHeight;
|
|
|
|
|
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
|
canvas.drawText(text, 0, Math.abs(fontMetrics.ascent), textPaint);
|
|
|
|
|
}
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|