本文整理汇总了Java中android.media.Image类的典型用法代码示例。如果您正苦于以下问题:Java Image类的具体用法?Java Image怎么用?Java Image使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Image类属于android.media包,在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: imageAvailable
import android.media.Image; //导入依赖的package包/类
/**
* 图像可用时截屏
*
* @param reader
* @param path
*@param callback @return
*/
private void imageAvailable(ImageReader reader, String path, IRecordShotCallback callback) {
mImageReaderLock.lock();
try{
Image image = reader.acquireLatestImage();
if(image == null) return;
int width = image.getWidth();
int height = image.getHeight();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
image.close();
//需要在这里释放图片否则会截取很多图片
release();
saveBitmap(path, bitmap, callback);
}finally {
mImageReaderLock.unlock();
}
}
示例2: YUV_420_888toNV21
import android.media.Image; //导入依赖的package包/类
private static byte[] YUV_420_888toNV21(Image image) {
byte[] nv21;
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
nv21 = new byte[ySize + uSize + vSize];
//U and V are swapped
yBuffer.get(nv21, 0, ySize);
vBuffer.get(nv21, ySize, vSize);
uBuffer.get(nv21, ySize + vSize, uSize);
return nv21;
}
示例3: onImageAvailable
import android.media.Image; //导入依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader) {
// mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile, mImages));
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
mImages.add(ByteString.copyFrom(bytes));
showToast(String.valueOf(mImages.size()) + "/" + String.valueOf(mTotal), Toast.LENGTH_LONG);
if(mImages.size() >= mTotal) {
showToast(String.valueOf("Calibrating..."), Toast.LENGTH_LONG);
final Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(new GrpcPostImageRunnable());
}
}
} finally {
if (image != null) {
image.close();
}
}
}
示例4: decodeToBitmap
import android.media.Image; //导入依赖的package包/类
private Bitmap decodeToBitmap(final Image img) {
Image.Plane[] planes = img.getPlanes();
if (planes[0].getBuffer() == null) {
return null;
}
int width = img.getWidth();
int height = img.getHeight();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
Bitmap bitmap = Bitmap.createBitmap(
width + rowPadding / pixelStride, height,
Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(planes[0].getBuffer());
img.close();
return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true);
}
示例5: onImageAvailable
import android.media.Image; //导入依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader) {
try (Image image = reader.acquireLatestImage()) {
if (image == null) {
throw new IllegalStateException();
}
if (image.getFormat() != ImageFormat.JPEG) {
Log.e(TAG, "Unexpected image format: %d", image.getFormat());
throw new IllegalStateException();
}
final byte[] capturedData = readCapturedData(image);
nativeOnPhotoTaken(mNativeVideoCaptureDeviceAndroid, mCallbackId, capturedData);
} catch (IllegalStateException ex) {
notifyTakePhotoError(mCallbackId);
return;
}
if (createPreviewObjectsAndStartPreview()) return;
nativeOnError(mNativeVideoCaptureDeviceAndroid, "Error restarting preview");
}
示例6: preprocessImage
import android.media.Image; //导入依赖的package包/类
public Bitmap preprocessImage(final Image image) {
if (image == null) {
return null;
}
Assert.assertEquals("Invalid size width", rgbFrameBitmap.getWidth(), image.getWidth());
Assert.assertEquals("Invalid size height", rgbFrameBitmap.getHeight(), image.getHeight());
if (croppedBitmap != null && rgbFrameBitmap != null) {
ByteBuffer bb = image.getPlanes()[0].getBuffer();
rgbFrameBitmap = BitmapFactory.decodeStream(new ByteBufferBackedInputStream(bb));
Helper.cropAndRescaleBitmap(rgbFrameBitmap, croppedBitmap, 0);
}
image.close();
// For debugging
if (SAVE_PREVIEW_BITMAP) {
Helper.saveBitmap(croppedBitmap);
}
return croppedBitmap;
}
示例7: onImageAvailable
import android.media.Image; //导入依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] imageBytes = new byte[buffer.remaining()];
buffer.get(imageBytes);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
final String text = runModel(bitmap);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(text);
}
});
} finally {
if (image != null) {
image.close();
}
}
}
示例8: preprocessImage
import android.media.Image; //导入依赖的package包/类
public Bitmap preprocessImage(final Image image) {
if (image == null) {
return null;
}
Assert.assertEquals("Invalid size width", rgbFrameBitmap.getWidth(), image.getWidth());
Assert.assertEquals("Invalid size height", rgbFrameBitmap.getHeight(), image.getHeight());
if (croppedBitmap != null && rgbFrameBitmap != null) {
ByteBuffer bb = image.getPlanes()[0].getBuffer();
rgbFrameBitmap = BitmapFactory.decodeStream(new ByteBufferBackedInputStream(bb));
Helper.cropAndRescaleBitmap(rgbFrameBitmap, croppedBitmap, 0);
}
image.close();
// For debugging
if (SAVE_PREVIEW_BITMAP) {
Helper.saveBitmap(croppedBitmap);
}
return croppedBitmap;
}
示例9: startCapture
import android.media.Image; //导入依赖的package包/类
/**
* 开始截图. 获取 image 面板上的图片.
*/
private void startCapture() {
Image image = null;
try {
try {
finalize();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
image = mImageReader.acquireLatestImage();
} catch (Exception e) {
e.printStackTrace();
}
if (image == null) {
L.e(" 获取 image 为空 结束..");
return;
} else {
SaveTask mSaveTask = new SaveTask();
AsyncTaskCompat.executeParallel(mSaveTask, image);
}
}
示例10: extract
import android.media.Image; //导入依赖的package包/类
/**
* Extracts the Y-Plane from the YUV_420_8888 image to creates a IntensityPlane.
* The actual plane data will be copied into the new IntensityPlane object.
*
* @throws IllegalArgumentException if the provided images is not in the YUV_420_888 format
*/
@NonNull
public static IntensityPlane extract(@NonNull Image img) {
if (img.getFormat() != ImageFormat.YUV_420_888) {
throw new IllegalArgumentException("image format must be YUV_420_888");
}
Image.Plane[] planes = img.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
byte[] yPlane = new byte[buffer.remaining()];
buffer.get(yPlane);
int yRowStride = planes[0].getRowStride();
return new IntensityPlane(img.getWidth(), img.getHeight(), yPlane, yRowStride);
}
示例11: setupPreviewSizeAndImageReader
import android.media.Image; //导入依赖的package包/类
/**
* lazily initialize ImageReader and select preview size
*/
private void setupPreviewSizeAndImageReader() {
if (previewSize == null) {
previewSize = cameraHelper.selectPreviewSize(openCamera);
}
if (imageReader == null) {
int maxImages = 2; // should be at least 2 according to ImageReader.acquireLatestImage() documentation
imageReader = ImageReader.newInstance(previewSize.getWidth(), previewSize.getHeight(), ImageFormat.YUV_420_888, maxImages);
imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image img = reader.acquireLatestImage();
if (img != null) {
// Make a in memory copy of the image to close the image from the reader as soon as possible.
// This helps the thread running the preview staying up to date.
IntensityPlane imgCopy = IntensityPlane.extract(img);
img.close();
int imageRotation = cameraHelper.getImageRotation(openCamera, getRelativeDisplayRotation());
presenter.onImageCaptured(imgCopy, imageRotation);
}
}
}, null);
}
}
示例12: processImage
import android.media.Image; //导入依赖的package包/类
public void processImage(Image image) {
/**
* Process image here
* Called on every frame of video
*/
if (!Tap.readyForNextFrame()) {
return;
}
Mat mat = ImageUtil.imageToBgr(image);
long frameTime = System.currentTimeMillis();
long t1 = System.currentTimeMillis();
List<Point> notUsedWhenDebug = Tap.getAll(mat,
canvasCameraDebug.getHandContours(), canvasCameraDebug.getFingerTips());
long t2 = System.currentTimeMillis();
CanvasUtil.setScreenHeight(ViewUtil.getScreenHeight(this));
canvasCameraDebug.updateInfo(
t2 - t1, frameTime - lastFrameTime, Tap.getProcessInterval()
);
lastFrameTime = frameTime;
}
示例13: yuvToBgr
import android.media.Image; //导入依赖的package包/类
public static Mat yuvToBgr(Image image, Mat yuvMat) {
Mat bgrMat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
//Log.d("TESTCALL", bgrMat.rows() + " " + bgrMat.cols());
Imgproc.cvtColor(yuvMat, bgrMat, Imgproc.COLOR_YUV2BGR_I420);
//Log.d("TESTCALL", yuvMat.rows() + " " + yuvMat.cols());
return bgrMat;
}
示例14: preprocessImage
import android.media.Image; //导入依赖的package包/类
public Bitmap preprocessImage(final Image image) {
if (image == null) {
return null;
}
Assert.assertEquals("Invalid size width", rgbFrameBitmap.getWidth(), image.getWidth());
Assert.assertEquals("Invalid size height", rgbFrameBitmap.getHeight(), image.getHeight());
if (croppedBitmap != null && rgbFrameBitmap != null) {
ByteBuffer bb = image.getPlanes()[0].getBuffer();
rgbFrameBitmap = BitmapFactory.decodeStream(new ByteBufferBackedInputStream(bb));
Helper.cropAndRescaleBitmap(rgbFrameBitmap, croppedBitmap, 0);
}
image.close();
// For debugging
if (SAVE_PREVIEW_BITMAP) {
Helper.saveBitmap(mContext, croppedBitmap);
}
return croppedBitmap;
}
示例15: onImageAvailable
import android.media.Image; //导入依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
// get image bytes
ByteBuffer imageBuf = image.getPlanes()[0].getBuffer();
final byte[] imageBytes = new byte[imageBuf.remaining()];
imageBuf.get(imageBytes);
image.close();
onPictureTaken(imageBytes);
}