本文整理汇总了Java中android.graphics.YuvImage类的典型用法代码示例。如果您正苦于以下问题:Java YuvImage类的具体用法?Java YuvImage怎么用?Java YuvImage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
YuvImage类属于android.graphics包,在下文中一共展示了YuvImage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: imgToByte
import android.graphics.YuvImage; //导入依赖的package包/类
private byte[] imgToByte(boolean quality) {
Camera.Parameters parameters = getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
YuvImage yuv = new YuvImage(getImage(), parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out =
new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
byte[] compressed = out.toByteArray();
Bitmap newBmp = BitmapFactory.decodeByteArray(compressed, 0, compressed.length);
Matrix mat = new Matrix();
mat.postRotate(PrefsController.instance.getPrefs().getCameraPrefs(cameraId).angle);
newBmp = Bitmap.createBitmap(newBmp, 0, 0, newBmp.getWidth(), newBmp.getHeight(), mat, true);
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
if (quality) {
newBmp.compress(Bitmap.CompressFormat.PNG, 100, out2);
} else {
newBmp.compress(Bitmap.CompressFormat.JPEG, 80, out2);
}
return out2.toByteArray();
}
示例2: createFromNV21
import android.graphics.YuvImage; //导入依赖的package包/类
public static byte[] createFromNV21(@NonNull final byte[] data,
final int width,
final int height,
int rotation,
final Rect croppingRect,
final boolean flipHorizontal)
throws IOException
{
byte[] rotated = rotateNV21(data, width, height, rotation, flipHorizontal);
final int rotatedWidth = rotation % 180 > 0 ? height : width;
final int rotatedHeight = rotation % 180 > 0 ? width : height;
YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21,
rotatedWidth, rotatedHeight, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
previewImage.compressToJpeg(croppingRect, 80, outputStream);
byte[] bytes = outputStream.toByteArray();
outputStream.close();
return bytes;
}
示例3: getBitmap
import android.graphics.YuvImage; //导入依赖的package包/类
private Bitmap getBitmap(Rect cropRect, int scaleFactor) {
if(isRotated()) {
//noinspection SuspiciousNameCombination
cropRect = new Rect(cropRect.top, cropRect.left, cropRect.bottom, cropRect.right);
}
// TODO: there should be a way to do this without JPEG compression / decompression cycle.
YuvImage img = new YuvImage(data, imageFormat, dataWidth, dataHeight, null);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
img.compressToJpeg(cropRect, 90, buffer);
byte[] jpegData = buffer.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options);
// Rotate if required
if (rotation != 0) {
Matrix imageMatrix = new Matrix();
imageMatrix.postRotate(rotation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), imageMatrix, false);
}
return bitmap;
}
示例4: convertYuvImageToRgb
import android.graphics.YuvImage; //导入依赖的package包/类
static public Bitmap convertYuvImageToRgb(YuvImage yuvImage, int width, int height, int downSample) {
Bitmap rgbImage;
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 0, out);
byte[] imageBytes = out.toByteArray();
BitmapFactory.Options opt;
opt = new BitmapFactory.Options();
opt.inSampleSize = downSample;
// get image and rotate it so (0,0) is in the bottom left
Bitmap tmpImage;
Matrix matrix = new Matrix();
matrix.postRotate(90); // to rotate the camera images so (0,0) is in the bottom left
tmpImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, opt);
rgbImage=Bitmap.createBitmap(tmpImage , 0, 0, tmpImage.getWidth(), tmpImage.getHeight(), matrix, true);
return rgbImage;
}
示例5: saveFace
import android.graphics.YuvImage; //导入依赖的package包/类
private void saveFace(final int x, final int y, final int r, final int b) {
if (DEBUG) Log.d(TAG, "[saveFace()]");
new Thread(new Runnable() {
@Override
public void run() {
synchronized (mVideoSource) {
mImageYuv = new YuvImage(mVideoSource, ImageFormat.NV21, CameraWrapper.IMAGE_WIDTH, CameraWrapper.IMAGE_HEIGHT, null);
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mImageYuv.compressToJpeg(new Rect(0, 0, CameraWrapper.IMAGE_WIDTH, CameraWrapper.IMAGE_HEIGHT), 100, stream);
Bitmap bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
int left = (x > 0) ? x : 0;
int top = (y > 0) ? y : 0;
int creatW = (r < CameraWrapper.IMAGE_WIDTH) ? (r - x) : (CameraWrapper.IMAGE_HEIGHT - x - 1);
int creatH = (b < CameraWrapper.IMAGE_WIDTH) ? (b - y) : (CameraWrapper.IMAGE_HEIGHT - y - 1);
mImage = Bitmap.createBitmap(bitmap, left, top, creatW, creatH, null, false);
if (DEBUG) Log.d(TAG, "[saveFace()] x:" + x + " y:" + y + "\n" +
"[saveFace()] h:" + mImage.getHeight() + " w:" + mImage.getWidth());
if (null != mImage)
FaceUtil.saveBitmapToFile(mImage);
}
}).start();
}
示例6: decodeToBitMap
import android.graphics.YuvImage; //导入依赖的package包/类
private Bitmap decodeToBitMap(byte[] data) {
try {
YuvImage image = new YuvImage(data, ImageFormat.NV21, PREVIEW_WIDTH,
PREVIEW_HEIGHT, null);
if (image != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, PREVIEW_WIDTH, PREVIEW_HEIGHT),
80, stream);
Bitmap bmp = BitmapFactory.decodeByteArray(
stream.toByteArray(), 0, stream.size());
stream.close();
return bmp ;
}
} catch (Exception ex) {
Log.e("Sys", "Error:" + ex.getMessage());
}
return null;
}
示例7: captureBitmapFromYuvFrame
import android.graphics.YuvImage; //导入依赖的package包/类
private Bitmap captureBitmapFromYuvFrame(I420Frame i420Frame) {
YuvImage yuvImage = i420ToYuvImage(i420Frame.yuvPlanes,
i420Frame.yuvStrides,
i420Frame.width,
i420Frame.height);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Rect rect = new Rect(0, 0, yuvImage.getWidth(), yuvImage.getHeight());
// Compress YuvImage to jpeg
yuvImage.compressToJpeg(rect, 100, stream);
// Convert jpeg to Bitmap
byte[] imageBytes = stream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
Matrix matrix = new Matrix();
// Apply any needed rotation
matrix.postRotate(i420Frame.rotationDegree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
true);
return bitmap;
}
示例8: fastI420ToYuvImage
import android.graphics.YuvImage; //导入依赖的package包/类
private YuvImage fastI420ToYuvImage(ByteBuffer[] yuvPlanes,
int[] yuvStrides,
int width,
int height) {
byte[] bytes = new byte[width * height * 3 / 2];
int i = 0;
for (int row = 0 ; row < height ; row++) {
for (int col = 0 ; col < width ; col++) {
bytes[i++] = yuvPlanes[0].get(col + row * yuvStrides[0]);
}
}
for (int row = 0 ; row < height / 2 ; row++) {
for (int col = 0 ; col < width / 2; col++) {
bytes[i++] = yuvPlanes[2].get(col + row * yuvStrides[2]);
bytes[i++] = yuvPlanes[1].get(col + row * yuvStrides[1]);
}
}
return new YuvImage(bytes, NV21, width, height, null);
}
示例9: yuv2Img
import android.graphics.YuvImage; //导入依赖的package包/类
/**
* yuv转换图像
* @param frameData
* @param yuvFormat
* @param prevWidth
* @param prevHeight
* @param quality
* @return
*/
public static Bitmap yuv2Img(byte[] frameData, int yuvFormat, int prevWidth, int prevHeight,
int quality) {
Long start = System.currentTimeMillis();
Bitmap img = null;
try {
YuvImage image = new YuvImage(frameData, yuvFormat, prevWidth, prevHeight, null);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
if (image != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, prevWidth, prevHeight), quality, stream);
img = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size(), options);
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
示例10: getFramePicture
import android.graphics.YuvImage; //导入依赖的package包/类
public byte[] getFramePicture(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int format = parameters.getPreviewFormat();
//YUV formats require conversion
if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
// Get the YuV image
YuvImage yuvImage = new YuvImage(data, format, w, h, null);
// Convert YuV to Jpeg
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(rect, 80, outputStream);
return outputStream.toByteArray();
}
return data;
}
示例11: onPictureTaken
import android.graphics.YuvImage; //导入依赖的package包/类
public void onPictureTaken(byte[] data, Camera camera) {
//new SaveImageTask().execute(data);
long b = System.currentTimeMillis()-a;
Log.d(TAG, "onPictureTaken - jpeg");
String str = "Data: " + data.length;
Log.d(TAG, str);
// Convert to JPG
Camera.Size previewSize = camera.getParameters().getPreviewSize();
YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 100, baos);
byte[] jdata = baos.toByteArray();
// Convert to Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
str = "BMP: " + bmp.getPixel(35, 84);
Log.d(TAG, str);
str = "Time: " + b;
Log.d(TAG, str);
resetCam();
}
示例12: onPreviewFrame
import android.graphics.YuvImage; //导入依赖的package包/类
public void onPreviewFrame(byte[] data, Camera camera){
Log.d("TAG", "frame1 "+data.length);
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null);
// Convert to Bitmap
final double [][] imgmat = imgpro.BufferedYUVImage2Mat(yuvimage.getYuvData(),
yuvimage.getWidth(), yuvimage.getHeight(), 640, 480);
List<Double> ld = imgpro.AnalyzeMat(imgmat, 0.6);
String logline = "points:";
for(Double p : ld)
logline += " " + (1-p);
Log.d("TAG", logline);
double [] f = new double[ld.size()];
for(int i = 0; i < f.length; i ++)
f[i] = Math.pow(2.0, ld.get(i) * 2) * 440.0;
play(f);
}
示例13: rawByteArray2RGBABitmap2
import android.graphics.YuvImage; //导入依赖的package包/类
public void rawByteArray2RGBABitmap2(FileOutputStream b)
{
int yuvi = yuv_w * yuv_h;
int uvi = 0;
byte[] yuv = new byte[yuv_w * yuv_h * 3 / 2];
System.arraycopy(y, 0, yuv, 0, yuvi);
for (int i = 0; i < yuv_h / 2; i++)
{
for (int j = 0; j < yuv_w / 2; j++)
{
yuv[yuvi++] = v[uvi];
yuv[yuvi++] = u[uvi++];
}
}
YuvImage yuvImage = new YuvImage(yuv, ImageFormat.NV21, yuv_w, yuv_h, null);
Rect rect = new Rect(0, 0, yuv_w, yuv_h);
yuvImage.compressToJpeg(rect, 100, b);
}
示例14: onPreviewFrame
import android.graphics.YuvImage; //导入依赖的package包/类
@Override
public void onPreviewFrame(final byte[] data, final Camera camera) {
if (mInitAck && mDataAck) {
mInitAck = false;
mDataAck = false;
previewMissedCount = 0;
ThreadHandler.getInstance().doInBackground(new Runnable() {
@Override
public void run() {
Camera.Size size = camera.getParameters().getPreviewSize();
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
yuvImage.compressToJpeg(new Rect(0, 0, size.width, size.height), 50, out);
byte[] imageBytes = out.toByteArray();
mBluetoothHandler.write(BluetoothHandler.DATA_START.getBytes());
mPendingImageBytes = imageBytes;
}
});
} else {
previewMissedCount++;
if (previewMissedCount > 50) {
mInitAck = true;
mDataAck = true;
}
}
}
示例15: convertToYuvImageLineByLine
import android.graphics.YuvImage; //导入依赖的package包/类
/**
* YuvImage converter (line by line).
* @param width The width of image.
* @param height The height of image.
* @param yuvStrides yuvStrides array.
* @param yuvPlanes yuvPlanes array.
* @return YuvImage data.
*/
public static YuvImage convertToYuvImageLineByLine(final int width, final int height, final int[] yuvStrides, final ByteBuffer[] yuvPlanes) {
byte[] bytes = new byte[width * height * 3 / 2];
byte[] yuvPlanes0 = yuvPlanes[0].array();
byte[] yuvPlanes1 = yuvPlanes[1].array();
byte[] yuvPlanes2 = yuvPlanes[2].array();
int i = 0;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
bytes[i++] = yuvPlanes0[col + row * yuvStrides[0]];
}
}
for (int row = 0; row < height / 2; row++) {
for (int col = 0; col < width / 2; col++) {
bytes[i++] = yuvPlanes2[col + row * yuvStrides[2]];
bytes[i++] = yuvPlanes1[col + row * yuvStrides[1]];
}
}
return new YuvImage(bytes, ImageFormat.NV21, width, height, null);
}