|
|
|
@ -2,8 +2,10 @@ package org.easydarwin.util;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.Matrix;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
|
import android.text.TextPaint;
|
|
|
|
|
|
|
|
|
|
public class YUVUtils {
|
|
|
|
@ -45,6 +47,8 @@ public class YUVUtils {
|
|
|
|
|
return newbm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int[] pixels = null;
|
|
|
|
|
|
|
|
|
|
//图片转YUV
|
|
|
|
|
public static byte[] getYUVByBitmap(Bitmap bitmap) {
|
|
|
|
|
if (bitmap == null) {
|
|
|
|
@ -55,18 +59,23 @@ public class YUVUtils {
|
|
|
|
|
int height = bitmap.getHeight() / 2 * 2;
|
|
|
|
|
|
|
|
|
|
int size = width * height;
|
|
|
|
|
|
|
|
|
|
int pixels[] = new int[size];
|
|
|
|
|
if (pixels == null) {
|
|
|
|
|
pixels = new int[size];
|
|
|
|
|
}
|
|
|
|
|
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
|
|
|
|
|
byte[] data = rgb2YCbCr420(pixels, width, height);
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte[] yuv = null;
|
|
|
|
|
|
|
|
|
|
public static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
|
|
|
|
|
int len = width * height;
|
|
|
|
|
//yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。
|
|
|
|
|
byte[] yuv = new byte[len * 3 / 2];
|
|
|
|
|
if (yuv == null) {
|
|
|
|
|
yuv = new byte[len * 3 / 2];
|
|
|
|
|
}
|
|
|
|
|
int y, u, v;
|
|
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
|
for (int j = 0; j < width; j++) {
|
|
|
|
@ -93,38 +102,23 @@ public class YUVUtils {
|
|
|
|
|
return yuv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Bitmap generateBitmap(String text, int textSizePx, int textColor) {
|
|
|
|
|
public static void generateBitmap(Bitmap bmp, 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;
|
|
|
|
|
// 宽高向上取偶数
|
|
|
|
|
width = (width + 1) / 2 * 2;
|
|
|
|
|
height = (height + 1) / 2 * 2;
|
|
|
|
|
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
|
Canvas canvas = new Canvas(bmp);
|
|
|
|
|
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
|
|
|
|
|
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;
|
|
|
|
|
// 宽高向上取偶数
|
|
|
|
|
width = (width + 1) / 2 * 2;
|
|
|
|
|
height = (height + 1) / 2 * 2;
|
|
|
|
|
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
|
Canvas canvas = new Canvas(bmp);
|
|
|
|
|
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
|
|
|
|
|
canvas.drawText(text, 0, Math.abs(fontMetrics.ascent), textPaint);
|
|
|
|
|
}
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|