本文整理汇总了Java中android.media.ImageReader类的典型用法代码示例。如果您正苦于以下问题:Java ImageReader类的具体用法?Java ImageReader怎么用?Java ImageReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ImageReader类属于android.media包,在下文中一共展示了ImageReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: imageAvailable
import android.media.ImageReader; //导入依赖的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: readyCamera
import android.media.ImageReader; //导入依赖的package包/类
public void readyCamera() {
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
String pickedCamera = getCamera(manager);
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
manager.openCamera(pickedCamera, cameraStateCallback, null);
imageReader = ImageReader.newInstance(videoWidth, videoHeight, 0x00000001 /*ImageFormat.YUV_420_888*/, 2 /* images buffered */);
imageReader.setOnImageAvailableListener(onImageAvailableListener, null);
Log.i(TAG, "imageReader created");
} catch (CameraAccessException e){
Log.e(TAG, e.getMessage());
}
}
示例3: onImageAvailable
import android.media.ImageReader; //导入依赖的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: createVirtualEnvironment
import android.media.ImageReader; //导入依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createVirtualEnvironment() {
dateFormat = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
strDate = dateFormat.format(new java.util.Date());
pathImage = Environment.getExternalStorageDirectory().getPath() + "/Pictures/";
nameImage = pathImage + strDate + ".png";
mMediaProjectionManager1 = (MediaProjectionManager) activity.getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mWindowManager1 = (WindowManager) activity.getApplication().getSystemService(Context.WINDOW_SERVICE);
windowWidth = ViewUtil.getScreenWidth(activity);
windowHeight = ViewUtil.getSceenHeight(activity);
metrics = new DisplayMetrics();
mWindowManager1.getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
mImageReader = ImageReader.newInstance(windowWidth, windowHeight, 0x1, 2); //ImageFormat.RGB_565
LogUtil.d(TAG, "prepared the virtual environment");
}
示例5: onImageAvailable
import android.media.ImageReader; //导入依赖的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: setImageListener
import android.media.ImageReader; //导入依赖的package包/类
private void setImageListener(Handler handler) {
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
if (mImageAvailable) {
return;
}
mLatestImage = reader.acquireLatestImage();
if (mLatestImage != null) {
mImageAvailable = true;
synchronized (mImageWaitingLock) {
mImageWaitingLock.notify();
}
}
}
}, handler);
}
示例7: onImageAvailable
import android.media.ImageReader; //导入依赖的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: createVirtualDisplay
import android.media.ImageReader; //导入依赖的package包/类
/****************************************** Factoring Virtual Display creation ****************/
private void createVirtualDisplay() {
// get width and height
Point size = new Point();
mDisplay.getRealSize(size);
mWidth = size.x;
mHeight = size.y;
mDisplayMetrics = new DisplayMetrics();
mDisplay.getRealMetrics(mDisplayMetrics);
if (SmingManager.getInstance().isSaveScreenshot() == false) {
if (mWidth > SmingManager.getInstance().getSmingHalfWidth()) {
float downScale = (float) SmingManager.getInstance().getSmingHalfWidth() / mWidth;
mWidth *= downScale;
mHeight *= downScale;
mHeight += 2;
}
}
// start capture reader
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
}
示例9: setupPreviewSizeAndImageReader
import android.media.ImageReader; //导入依赖的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);
}
}
示例10: initCapture
import android.media.ImageReader; //导入依赖的package包/类
public void initCapture(Context context, MediaProjection mediaProjection, final String path, final IRecordShotCallback callback){
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
// thread for image checking
mCheckThread = new HandlerThread("CheckHandler");
mCheckThread.start();
mCheckHandler = new Handler(mCheckThread.getLooper());
try {
mImageReader = ImageReader.newInstance(screenWidth, screenHeight, PixelFormat.RGBA_8888, 2);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
imageAvailable(reader, path, callback);
}
}, mCheckHandler);
mSurface = mImageReader.getSurface();
}finally {
}
mVirtualDisplay = mediaProjection.createVirtualDisplay("mediaprojection", screenWidth, screenHeight,
1, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mSurface, null, null);
}
示例11: Shotter
import android.media.ImageReader; //导入依赖的package包/类
public Shotter(Context context, Intent data) {
this.mRefContext = new SoftReference<>(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mMediaProjection = getMediaProjectionManager().getMediaProjection(Activity.RESULT_OK,
data);
mImageReader = ImageReader.newInstance(
getScreenWidth(),
getScreenHeight(),
PixelFormat.RGBA_8888,//此处必须和下面 buffer处理一致的格式 ,RGB_565在一些机器上出现兼容问题。
1);
}
}
示例12: onImageAvailable
import android.media.ImageReader; //导入依赖的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);
}
示例13: onOpened
import android.media.ImageReader; //导入依赖的package包/类
@Override
public void onOpened(final CameraDevice cameraDevice, final ImageReader imageReader,
final Handler cameraHandler, final Rotation rotation, final boolean flipHorizontal,
final boolean flipVertical) {
if (!isResumed() || !CheckUtil.nonNull(mProcessorChain)) {
return;
}
mProcessorChain.onCameraOpened(rotation, flipHorizontal, flipVertical,
surfaceTexture -> {
// fix MX5 preview not show bug: http://stackoverflow.com/a/34337226/3077508
surfaceTexture.setDefaultBufferSize(mPreviewWidth, mPreviewHeight);
Surface surface = new Surface(surfaceTexture);
List<Surface> targets = Arrays.asList(surface, imageReader.getSurface());
mCamera2Helper.outputTargetChanged(targets);
startPreviewDirectly(cameraDevice, targets, mIsDefaultFlashOpen, cameraHandler);
});
}
示例14: onImageAvailable
import android.media.ImageReader; //导入依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader)
{
Activity activity = getActivity();
if(null == mTextureView || null == mPreviewSize || null == activity)
{
return;
}
try
{
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(),
getJpegOrientation(characteristics, rotation)));
}
catch(CameraAccessException e)
{
LogExt.e(getClass(), e);
}
}
示例15: onImageAvailable
import android.media.ImageReader; //导入依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
try {
if (image.getFormat() == ImageFormat.JPEG) {
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
Bitmap rotated = ImageUtils.rotateBitmap(bitmap, rotation, mCamera2Engine.getSensorOrientation());
mImageView.setImageBitmap(rotated);
}
} finally {
image.close();
}
}