注意点:
1.使用camera时,画面始终无法旋转。
有可能是自己还不太熟悉的原因。
最终使用了camera1+TextureView,
因为这个Camera.setDisplayOrientation
API很香啊。哈哈
2.onPreviewFrame得到的数据 要进行翻转的。
public class NV21Utils {
//如果进行编码,注意90、270时,宽高要进行对换。
public static byte[] NV21_rotate_to_270(byte[] nv21_data, int width, int height) {
int y_size = width * height;
int buffser_size = y_size * 3 / 2;
byte[] nv21_rotated = new byte[buffser_size];
int i = 0;
// Rotate the Y luma
for (int x = width - 1; x >= 0; x--) {
int offset = 0;
for (int y = 0; y < height; y++) {
nv21_rotated[i] = nv21_data[offset + x];
i++;
offset += width;
}
}
// Rotate the U and V color components
i = y_size;
for (int x = width - 1; x > 0; x = x - 2) {
int offset = y_size;
for (int y = 0; y < height / 2; y++) {
nv21_rotated[i] = nv21_data[offset + (x - 1)];
i++;
nv21_rotated[i] = nv21_data[offset + x];
i++;
offset += width;
}
}
return nv21_rotated;
}
public static byte[] NV21_rotate_to_180(byte[] nv21_data, int width, int height) {
int y_size = width * height;
int buffser_size = y_size * 3 / 2;
byte[] nv21_rotated = new byte[buffser_size];
int i = 0;
int count = 0;
for (i = y_size - 1; i >= 0; i--) {
nv21_rotated[count] = nv21_data[i];
count++;
}
for (i = buffser_size - 1; i >= y_size; i -= 2) {
nv21_rotated[count++] = nv21_data[i - 1];
nv21_rotated[count++] = nv21_data[i];
}
return nv21_rotated;
}
public static byte[] NV21_rotate_to_90(byte[] nv21_data, int width, int height) {
int y_size = width * height;
int buffser_size = y_size * 3 / 2;
byte[] nv21_rotated = new byte[buffser_size];
// Rotate the Y luma
int i = 0;
int startPos = (height - 1) * width;
for (int x = 0; x < width; x++) {
int offset = startPos;
for (int y = height - 1; y >= 0; y--) {
nv21_rotated[i] = nv21_data[offset + x];
i++;
offset -= width;
}
}
// Rotate the U and V color components
i = buffser_size - 1;
for (int x = width - 1; x > 0; x = x - 2) {
int offset = y_size;
for (int y = 0; y < height / 2; y++) {
nv21_rotated[i] = nv21_data[offset + x];
i--;
nv21_rotated[i] = nv21_data[offset + (x - 1)];
i--;
offset += width;
}
}
return nv21_rotated;
}
}
需要注意的是。如果使用了之前摄像头翻转了90或者270,那么这里的宽高需要对换
也就是说,编码出来的byte[],你如果想要转换成bitmap,那么你之前的width和height需要调换位置。
3.onPreviewFrame出来的数据太快了。灵魂追不上
我使用了rxjava的方式进行拦截,1S执行一次
Observable.create(ObservableOnSubscribe<ByteArray> { emitter ->
sender = emitter
}).throttleLast(1, TimeUnit.SECONDS)
.subscribe(sendDataObserver!!)
override fun onPreviewFrame(data: ByteArray, camera: Camera) {
val params = camera.parameters.previewSize;
if (mWidth != params.width) {
Logger.i("尺寸不一样,重新进行校正")
mWidth = params.width;
mHeight = params.height;
}
sender?.onNext(data);
}
网友评论