Java Code Examples for android.media.ImageReader#newInstance()

The following examples show how to use android.media.ImageReader#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CatVision.java    From CatVision-io-SDK-Android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/******************************************
 * Factoring Virtual Display creation
 ****************/
@TargetApi(minAPILevel)
private void createVirtualDisplay() {
	if (android.os.Build.VERSION.SDK_INT < minAPILevel) {
		Log.w(TAG, "Can't run createVirtualDisplay() due to a low API level. API level 21 or higher is required.");
		return;
	}
	// get width and height
	Point size = new Point();
	mDisplay.getRealSize(size);
	int mWidth = (int)(size.x / downscale);
	int mHeight = (int)(size.y / downscale);

	if (vncServer != null) {
		vncServer.shutdown();
		vncServer.run(mWidth, mHeight);
	}
	startRepeatingPing();

	int VIRTUAL_DISPLAY_FLAGS = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;

	// run capture reader
	mImageReader = ImageReader.newInstance(mWidth, mHeight, mMediaProjectionPixelFormat, 2);
	mVirtualDisplay = sMediaProjection.createVirtualDisplay("cvio", mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
	mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
}
 
Example 2
Source File: ImageCapture.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 *
 */
void initImageRead(MediaProjection mediaProjection) {
    if (mediaProjection == null) {
        LogHelper.e(TAG, "mediaProjection == null");
        return;
    }
    int width = UIUtils.getWidthPixels();
    int height = UIUtils.getRealHeightPixels();
    int dpi = UIUtils.getDensityDpi();
    mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
    /**
     * 获取getSurface
     */
    mediaProjection.createVirtualDisplay("ScreenCapture",
            width, height, dpi,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
            mImageReader.getSurface(), null, null);
}
 
Example 3
Source File: Camera2Proxy.java    From CameraDemo with Apache License 2.0 6 votes vote down vote up
@SuppressLint("MissingPermission")
public void openCamera(int width, int height) {
    Log.v(TAG, "openCamera");
    startBackgroundThread(); // 对应 releaseCamera() 方法中的 stopBackgroundThread()
    mOrientationEventListener.enable();
    try {
        mCameraCharacteristics = mCameraManager.getCameraCharacteristics(Integer.toString(mCameraId));
        StreamConfigurationMap map = mCameraCharacteristics.get(CameraCharacteristics
                .SCALER_STREAM_CONFIGURATION_MAP);
        // 拍照大小,选择能支持的一个最大的图片大小
        Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new
                CompareSizesByArea());
        Log.d(TAG, "picture size: " + largest.getWidth() + "*" + largest.getHeight());
        mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, 2);
        // 预览大小,根据上面选择的拍照图片的长宽比,选择一个和控件长宽差不多的大小
        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, largest);
        Log.d(TAG, "preview size: " + mPreviewSize.getWidth() + "*" + mPreviewSize.getHeight());
        // 打开摄像头
        mCameraManager.openCamera(Integer.toString(mCameraId), mStateCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: CameraConfig.java    From AndroidDemo with MIT License 5 votes vote down vote up
public void setImageFormat(int imageFormat) {
    if (streamConfigurationMap.isOutputSupportedFor(imageFormat)) {

        imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), imageFormat, 1);
        imageReader.setOnImageAvailableListener(imageAvailableListener, handler);
    } else {
        Log.w(TAG, "ImageFormat <" + imageFormat + "> is not support.");
    }
}
 
Example 5
Source File: CameraHandler.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
@SuppressLint("MissingPermission")
public void initializeCamera(Context context, int previewWidth, int previewHeight,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    if (initialized) {
        throw new IllegalStateException(
                "CameraHandler is already initialized or is initializing");
    }
    initialized = true;

    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    String[] camIds = null;
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.w(TAG, "Cannot get the list of available cameras", e);
    }
    if (camIds == null || camIds.length < 1) {
        Log.d(TAG, "No cameras found");
        return;
    }
    Log.d(TAG, "Using camera id " + camIds[0]);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(previewWidth, previewHeight, ImageFormat.JPEG,
            MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(camIds[0], mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 6
Source File: RecordService.java    From screen-share-to-browser with Apache License 2.0 5 votes vote down vote up
@Override
    public void onCreate() {
        super.onCreate();
        HandlerThread serviceThread = new HandlerThread("service_thread", android.os.Process.THREAD_PRIORITY_BACKGROUND);
        serviceThread.start();
        running = false;

        threadCount = Runtime.getRuntime().availableProcessors();
        Log.d(TAG, "onCreate: threadCount" + threadCount);
        executorService = Executors.newFixedThreadPool(threadCount);

        HandlerThread handlerThread = new HandlerThread("Screen Record");
        handlerThread.start();
        screenHandler = new ScreenHandler(handlerThread.getLooper());

        //get the size of the window
        WindowManager mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
//        width = mWindowManager.getDefaultDisplay().getWidth() + 40;
        width = mWindowManager.getDefaultDisplay().getWidth();
        height = mWindowManager.getDefaultDisplay().getHeight();
        //height = 2300;
        Log.i(TAG, "onCreate: w is " + width + " h is " + height);
        imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);

        scheduler = Schedulers.from(executorService);
//        Disposable disposable =
//                getByteBufferObservable()
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(scheduler)
//                        .map(getBitmapFunction())
//                        .subscribe(getBitmapConsumer());
//        compositeDisposable.add(disposable);
    }
 
Example 7
Source File: DoorbellCamera.java    From androidthings-cameraCar with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
public void initializeCamera(Context context,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
    String[] camIds = {};
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.d(TAG, "Cam access exception getting IDs", e);
    }
    if (camIds.length < 1) {
        Log.d(TAG, "No cameras found");
        return;
    }
    String id = camIds[0];
    Log.d(TAG, "Using camera id " + id);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(IMAGE_WIDTH, IMAGE_HEIGHT,
            ImageFormat.JPEG, MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(
            imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(id, mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 8
Source File: Camera2.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
private void prepareImageReader() {
    if (mImageReader != null) {
        mImageReader.close();
    }
    Size largest = mPictureSizes.sizes(mAspectRatio).last();
    mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
            ImageFormat.JPEG, /* maxImages */ 2);
    mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
}
 
Example 9
Source File: Vr2dDisplay.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Starts an ImageReader as a do-nothing Surface.  The virtual display will not get fully
 * initialized within surface flinger unless it has a valid Surface associated with it. We use
 * the ImageReader as the default valid Surface.
 */
private void startImageReader() {
    if (mImageReader == null) {
        mImageReader = ImageReader.newInstance(mVirtualDisplayWidth, mVirtualDisplayHeight,
            PixelFormat.RGBA_8888, 2 /* maxImages */);
        Log.i(TAG, "VD startImageReader: res = " + mVirtualDisplayWidth + "X" +
                mVirtualDisplayHeight + ", dpi = " + mVirtualDisplayDpi);
    }
    synchronized (mVdLock) {
        setSurfaceLocked(mImageReader.getSurface());
    }
}
 
Example 10
Source File: ScreenCapturer.java    From habpanelviewer with GNU General Public License v3.0 5 votes vote down vote up
public synchronized Bitmap captureScreen() throws IllegalStateException {
    AtomicReference<Image> imageHolder = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);

    ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
    mImageReader.setOnImageAvailableListener(imageReader -> {
        imageHolder.set(mImageReader.acquireLatestImage());
        latch.countDown();
    }, mHandler);

    VirtualDisplay display = mProjection.createVirtualDisplay("screen-mirror", mWidth, mHeight, mDensity,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mImageReader.getSurface(),
            null, null);

    try {
        latch.await(1, TimeUnit.SECONDS);

        if (latch.getCount() == 1) {
            throw new IllegalStateException("Screen capturing timed out");
        }

        final Image image = imageHolder.get();
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();
        int pixelStride = planes[0].getPixelStride();
        int rowStride = planes[0].getRowStride();
        int rowPadding = rowStride - pixelStride * mWidth;

        // create bitmap
        Bitmap bmp = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
        bmp.copyPixelsFromBuffer(buffer);
        image.close();

        return bmp;
    } catch (InterruptedException e) {
        throw new IllegalStateException("Got interrupt while capturing screen");
    } finally {
        display.release();
    }
}
 
Example 11
Source File: WindowCaptureFragment.java    From ViewCapture with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    mScreenDensity = metrics.densityDpi;
    windowWidth = metrics.widthPixels;
    windowHeight = metrics.heightPixels;
    mImageReader = ImageReader.newInstance(windowWidth, windowHeight, PixelFormat.RGBA_8888, 2);
    mMediaProjectionManager = (MediaProjectionManager) getActivity().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
}
 
Example 12
Source File: Camera2.java    From TikTok with Apache License 2.0 5 votes vote down vote up
private void prepareImageReader() {
    if (mImageReader != null) {
        mImageReader.close();
    }
    Size largest = mPictureSizes.sizes(mAspectRatio).last();
    mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
            ImageFormat.JPEG, /* maxImages */ 2);
    mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
}
 
Example 13
Source File: Camera2Input.java    From EZFilter with MIT License 4 votes vote down vote up
public Camera2Input(IGLEnvironment render, CameraDevice camera, Size previewSize) {
    super();
    this.mRender = render;
    this.mCamera = camera;
    this.mPreviewSize = previewSize;

    setVertexShader("uniform mat4 " + UNIFORM_CAM_MATRIX + ";\n"
            + "attribute vec4 " + ATTRIBUTE_POSITION + ";\n"
            + "attribute vec2 " + ATTRIBUTE_TEXTURE_COORD + ";\n"
            + "varying vec2 " + VARYING_TEXTURE_COORD + ";\n"
            + "void main() {\n"
            + "   vec4 texPos = " + UNIFORM_CAM_MATRIX + " * vec4(" + ATTRIBUTE_TEXTURE_COORD + ", 1, 1);\n"
            + "   " + VARYING_TEXTURE_COORD + " = texPos.xy;\n"
            + "   gl_Position = " + ATTRIBUTE_POSITION + ";\n"
            + "}\n");
    setFragmentShader("#extension GL_OES_EGL_image_external : require\n"
            + "precision mediump float;\n"
            + "uniform samplerExternalOES " + UNIFORM_TEXTURE_0 + ";\n"
            + "varying vec2 " + VARYING_TEXTURE_COORD + ";\n"
            + "void main() {\n"
            + "   gl_FragColor = texture2D(" + UNIFORM_TEXTURE_0 + ", " + VARYING_TEXTURE_COORD + ");\n"
            + "}\n");

    mImageReader = ImageReader.newInstance(previewSize.getHeight(), previewSize.getWidth(), ImageFormat.JPEG, 1);
    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(final ImageReader reader) {
            new Thread() {
                public void run() {
                    // 1.读取原始图片旋转信息
                    Image image = reader.acquireNextImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] data = new byte[buffer.remaining()];
                    buffer.get(data);
                    image.close();
                    int originalDegree = CameraUtil.getExifDegree(data);
                    // 2.加载原始图片
                    final Bitmap bitmap0 = BitmapFactory.decodeByteArray(data, 0, data.length);
                    // 3.旋转及镜像原始图片
                    Matrix matrix = new Matrix();
                    if (mIsFront) {
                        matrix.postScale(-1, 1);
                    }
                    matrix.postRotate(mDegree - originalDegree);
                    final Bitmap bitmap1 = Bitmap.createBitmap(bitmap0, 0, 0,
                            bitmap0.getWidth(), bitmap0.getHeight(), matrix, true);

                    // 由于bitmap1可能与bitmap0是同一个对象,这里进行判断
                    if (bitmap1 != bitmap0) {
                        bitmap0.recycle();
                    }

                    if (mPhotoTakenCallback != null) {
                        mPhotoTakenCallback.onPhotoTaken(bitmap1);
                    }
                }
            }.start();
        }
    }, mPreviewHandler);
}
 
Example 14
Source File: CameraActivity.java    From Chinese-number-gestures-recognition with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@SuppressLint("NewApi")
    private void initCamera2() {
        findViewById(R.id.btn_control).setClickable(true);

        HandlerThread handlerThread = new HandlerThread("Camera2");
        handlerThread.start();
        childHandler = new Handler(handlerThread.getLooper());
        mainHandler = new Handler(getMainLooper());


        mCameraID = BACK_CAMERA;//后摄像头
        Log.d(TAG, "initCamera2: "+ CameraCharacteristics.LENS_FACING_BACK);
        mImageReader = ImageReader.newInstance(1080, 1920, ImageFormat.JPEG,1);
        mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            //可以在这里处理拍照得到的临时照片 例如,写入本地
            @Override
            public void onImageAvailable(ImageReader reader) {
//                mCameraDevice.close();
//                mSurfaceView.setVisibility(View.INVISIBLE);
                // 拿到拍照照片数据
                Image image = reader.acquireNextImage();
                ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                byte[] bytes = new byte[buffer.remaining()];
                buffer.get(bytes);//由缓冲区存入字节数组
                //保存

                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                if (bitmap != null) {
                    if(mCameraID == FRONT_CAMERA){
                        //前置摄像头拍的要先旋转180度
                        bitmap = adjustPhotoRotation(bitmap,180);
                    }
                    iv_thumb.setImageBitmap(bitmap);
                    writeToFile(bitmap);
                }

                image.close();

                //缩放得到用于显示的图片 128*128
                Bitmap displayBitmap = scaleImage(bitmap,128,128);
                //缩放得到用于预测的图片 64*64
                Bitmap bitmapForPredit = scaleImage(bitmap,64,64);

//                //把图片保存到本地图库
                //MediaStore.Images.Media.insertImage(getContentResolver(), bitmapForPredit, "title", "description");

                //加载模型
                classifier = new Classifier(getAssets(),MODEL_FILE);
                ArrayList<String> result = classifier.predict(bitmapForPredit);
                //传递参数
                Bundle bundle = new Bundle();
                bundle.putParcelable("image",displayBitmap);
                bundle.putStringArrayList("recognize_result",result);
                Intent intent = new Intent(CameraActivity.this, DisplayResult.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }

        }, mainHandler);
        //获取摄像头管理
        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

            //打开摄像头
            mCameraManager.openCamera(mCameraID+"", stateCallback, mainHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
 
Example 15
Source File: Camera2.java    From MediaPickerInstagram with Apache License 2.0 4 votes vote down vote up
private void prepareImageReader() {
    Size largest = mPictureSizes.sizes(mAspectRatio).last();
    mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
            ImageFormat.JPEG, /* maxImages */ 2);
    mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
}
 
Example 16
Source File: OneCameraZslImpl.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiates a new camera based on Camera 2 API.
 *
 * @param device          The underlying Camera 2 device.
 * @param characteristics The device's characteristics.
 * @param pictureSize     the size of the final image to be taken.
 */
OneCameraZslImpl(CameraDevice device, CameraCharacteristics characteristics, Size pictureSize)
{
    Log.v(TAG, "Creating new OneCameraZslImpl");

    mDevice = device;
    mCharacteristics = characteristics;
    mLensRange = LensRangeCalculator
            .getDiopterToRatioCalculator(characteristics);
    mDirection = new CameraDirectionProvider(mCharacteristics);
    mFullSizeAspectRatio = calculateFullSizeAspectRatio(characteristics);

    mCameraThread = new HandlerThread("OneCamera2");
    // If this thread stalls, it will delay viewfinder frames.
    mCameraThread.setPriority(Thread.MAX_PRIORITY);
    mCameraThread.start();
    mCameraHandler = new Handler(mCameraThread.getLooper());

    mCameraListenerThread = new HandlerThread("OneCamera2-Listener");
    mCameraListenerThread.start();
    mCameraListenerHandler = new Handler(mCameraListenerThread.getLooper());

    // TODO: Encoding on multiple cores results in preview jank due to
    // excessive GC.
    int numEncodingCores = CameraUtil.getNumCpuCores();
    mImageSaverThreadPool = new ThreadPoolExecutor(numEncodingCores, numEncodingCores, 10,
            TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

    mCaptureManager =
            new ImageCaptureManager(MAX_CAPTURE_IMAGES, mCameraListenerHandler,
                    mImageSaverThreadPool);
    mCaptureManager.setCaptureReadyListener(new ImageCaptureManager.CaptureReadyListener()
    {
        @Override
        public void onReadyStateChange(boolean capturePossible)
        {
            mReadyStateManager.setInput(ReadyStateRequirement.CAPTURE_MANAGER_READY,
                    capturePossible);
        }
    });

    // Listen for changes to auto focus state and dispatch to
    // mFocusStateListener.
    mCaptureManager.addMetadataChangeListener(CaptureResult.CONTROL_AF_STATE,
            new ImageCaptureManager.MetadataChangeListener()
            {
                @Override
                public void onImageMetadataChange(Key<?> key, Object oldValue, Object newValue,
                                                  CaptureResult result)
                {
                    FocusStateListener listener = mFocusStateListener;
                    if (listener != null)
                    {
                        listener.onFocusStatusUpdate(
                                AutoFocusHelper.stateFromCamera2State(
                                        result.get(CaptureResult.CONTROL_AF_STATE)),
                                result.getFrameNumber());
                    }
                }
            });

    // Allocate the image reader to store all images received from the
    // camera.
    if (pictureSize == null)
    {
        // TODO The default should be selected by the caller, and
        // pictureSize should never be null.
        pictureSize = getDefaultPictureSize();
    }
    mCaptureImageReader = ImageReader.newInstance(pictureSize.getWidth(),
            pictureSize.getHeight(),
            sCaptureImageFormat, MAX_CAPTURE_IMAGES);

    mCaptureImageReader.setOnImageAvailableListener(mCaptureManager, mCameraHandler);
    mMediaActionSound.load(MediaActionSound.SHUTTER_CLICK);
}
 
Example 17
Source File: Camera2Controller.java    From pixelvisualcorecamera with Apache License 2.0 4 votes vote down vote up
private void configureImageReader(Size s) {
  imageReader = ImageReader.newInstance(s.getWidth(), s.getHeight(),
      ImageFormat.JPEG, /*maxImages*/2);
  imageReader.setOnImageAvailableListener(onImageAvailableListener, backgroundHandler);
}
 
Example 18
Source File: SRManager.java    From VMLibrary with Apache License 2.0 4 votes vote down vote up
/**
 * 初始化读取屏幕截图 ImageReader
 */
private void initImageReader() {
    imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 1);
    imageReader.setOnImageAvailableListener(new ImageAvailableListener(), null);

}
 
Example 19
Source File: CaptureManager.java    From FastBarcodeScanner with Apache License 2.0 4 votes vote down vote up
public void setup(String cameraId, int outputFormat, int imageWidth, int imageHeight) {
    try {
        /*CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
        StreamConfigurationMap map = manager
                .getCameraCharacteristics(cameraId)
                .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        if (map == null) {
            throw new UnsupportedOperationException("Insufficient camera info available");
        }

        // Check the output format:
        // ========================
        if (map.isOutputSupportedFor(imageFormat) == false)
            throw new UnsupportedOperationException("Camera cannot capture images in format " + imageFormat);
        int outputFormat = imageFormat;


        // Choose an image size:
        // =====================
        List<Size> choices = Arrays.asList(map.getOutputSizes(outputFormat));

        // We'll prefer the smallest size larger than mMinPixels (default 1024*768)
        List<Size> bigEnough = new ArrayList<>();
        for (Size option : choices) {
            if (option.getWidth() * option.getHeight() >= mMinPixels) {
                bigEnough.add(option);
            }
        }

        Size captureSize = null;
        if (bigEnough.isEmpty())
            captureSize = Collections.max(choices, new CompareSizesByArea());
        else
            captureSize = Collections.min(bigEnough, new CompareSizesByArea());
        */

        // Set up the still image reader:
        // ==============================
        mImageReader = ImageReader.newInstance(
                imageWidth,
                imageHeight,
                outputFormat,
                /*maxImages*/4
        );
    //} catch (CameraAccessException e) {
    //    e.printStackTrace();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        Log.e(TAG, "Camera2 API is not supported");
        throw new UnsupportedOperationException("Camera2 API is not supported");
    }
}
 
Example 20
Source File: Camera2Helper.java    From DeviceConnect-Android with MIT License 2 votes vote down vote up
/**
 * ImageReader のインスタンスを作成します.
 *
 * @param width 横幅
 * @param height 縦幅
 * @param imageFormat 写真のフォーマット
 * @return ImageReader のインスタンス
 */
public static ImageReader createImageReader(final int width, final int height, final int imageFormat) {
    return ImageReader.newInstance(width, height, imageFormat, 1);
}