本文整理汇总了Java中android.hardware.camera2.CameraManager类的典型用法代码示例。如果您正苦于以下问题:Java CameraManager类的具体用法?Java CameraManager怎么用?Java CameraManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CameraManager类属于android.hardware.camera2包,在下文中一共展示了CameraManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreate
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_v2);
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
try {
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing.equals(CameraCharacteristics.LENS_FACING_FRONT)) {
}
// Do something with the characteristics
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
示例2: readyCamera
import android.hardware.camera2.CameraManager; //导入依赖的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: doesUserHaveFrontFacingCamera
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
/**
* Checks if user has front facing camera.
* @param context Context used to check
* @return 0 For front facing, 1 for back facing, -1 for if it does not have front facing at all
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static int doesUserHaveFrontFacingCamera(Context context) {
if(Build.VERSION.SDK_INT >= 21) {
try {
CameraManager cManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
for (int j = 0; j < cManager.getCameraIdList().length; j++) {
String[] cameraId = cManager.getCameraIdList();
CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId[j]);
int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
if (cOrientation == CameraCharacteristics.LENS_FACING_FRONT)
return j;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
return -1; // No front-facing camera found
}
示例4: open
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
public void open() {
try {
CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mCameraSize = map.getOutputSizes(SurfaceTexture.class)[0];
HandlerThread thread = new HandlerThread("OpenCamera");
thread.start();
Handler backgroundHandler = new Handler(thread.getLooper());
manager.openCamera(cameraId, mCameraDeviceCallback, null);
// カメラの物理的な情報を得る
mCameraCharacteristics = manager.getCameraCharacteristics( cameraId );
return;
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
示例5: setupCameraIfNeeded
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
private boolean setupCameraIfNeeded() {
if (cameraId != null) {
return true;
}
try {
cameraId = cameraManager.getCameraIdList()[0]; // Usually back camera is at 0 position
} catch (CameraAccessException e) {
Log.v(TAG, "failed to set up camera");
return false;
}
torchEnabled = false;
Handler handler = new Handler();
torchCallback = new CameraManager.TorchCallback() {
@Override
public void onTorchModeChanged(@NonNull String cameraId, boolean enabled) {
super.onTorchModeChanged(cameraId, enabled);
torchEnabled = enabled;
}
};
cameraManager.registerTorchCallback(torchCallback, handler);
Log.v(TAG, "registered torch callback");
return true;
}
示例6: CameraOps
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
/**
* Create a new camera ops thread.
*
* @param errorDisplayer listener for displaying error messages
* @param readyListener listener for notifying when camera is ready for requests
* @param readyHandler the handler for calling readyListener methods on
*/
CameraOps(CameraManager manager, ErrorDisplayer errorDisplayer,
CameraReadyListener readyListener, Handler readyHandler) {
mCameraThread = new HandlerThread("CameraOpsThread");
mCameraThread.start();
mCameraHandler = new Handler(mCameraThread.getLooper());
if (manager == null || errorDisplayer == null || readyListener == null ||
readyHandler == null) {
throw new IllegalArgumentException("Need valid displayer, listener, handler");
}
mCameraManager = manager;
mErrorDisplayer = errorDisplayer;
mReadyListener = readyListener;
mReadyHandler = readyHandler;
}
示例7: getCameraInfo
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
@Override
protected String getCameraInfo() {
String camStr = mContext.getString(R.string.camApi2) + "\n";
CameraManager camManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
try {
for (String camId : camManager.getCameraIdList()) {
camStr += mContext.getString(R.string.cameraId, camId) + ": ";
CameraCharacteristics characteristics = camManager.getCameraCharacteristics(camId);
Boolean hasFlash = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
camStr += (hasFlash ? mContext.getString(R.string.has) : mContext.getString(R.string.no))
+ " " + mContext.getString(R.string.flash) + ", ";
camStr += (facing == CameraCharacteristics.LENS_FACING_BACK ?
mContext.getString(R.string.backFacing) : mContext.getString(R.string.frontFacing)) + "\n";
}
} catch (CameraAccessException e) {
camStr = mActivity.getString(R.string.failedAccessCamera) + ":" + e.getMessage();
}
return camStr.trim();
}
示例8: FlashController
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
public FlashController(Context ctx, CameraManager cameraManager, ServerConnection serverConnection) throws CameraAccessException, IllegalAccessException {
mCtx = ctx;
camManager = cameraManager;
mServerConnection = serverConnection;
EventBus.getDefault().register(this);
for (String camId : camManager.getCameraIdList()) {
CameraCharacteristics characteristics = camManager.getCameraCharacteristics(camId);
Boolean hasFlash = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing == CameraCharacteristics.LENS_FACING_BACK && hasFlash) {
torchId = camId;
break;
}
}
if (torchId == null) {
throw new IllegalAccessException(ctx.getString(R.string.couldNotFindBackFlash));
}
}
示例9: collectRatioSizes
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
@Override
public void collectRatioSizes() {
ratioSizeList.clear();
CameraCharacteristics characteristics;
StreamConfigurationMap map = null;
try {
characteristics = ((CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE)).getCameraCharacteristics(Integer.toString(Integer.parseInt(getCameraId())));
map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Size[] outputSizes = map.getOutputSizes(SurfaceTexture.class);
if (outputSizes != null) {
List<Double> ratioList = new ArrayList<>();
for (Size size : outputSizes) {
double ratio = (double) size.getWidth() / (double) size.getHeight();
if (!ratioList.contains(ratio)) {
ratioList.add(ratio);
ratioSizeList.add(new AspectRatio(ratio, size.getWidth(), size.getHeight()));
}
}
}
}
示例10: Camera2Session
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
private Camera2Session(CreateSessionCallback callback, Events events, Context applicationContext,
CameraManager cameraManager, SurfaceTextureHelper surfaceTextureHelper,
MediaRecorder mediaRecorder, String cameraId, int width, int height, int framerate) {
Logging.d(TAG, "Create new camera2 session on camera " + cameraId);
videoFrameEmitTrialEnabled =
PeerConnectionFactory.fieldTrialsFindFullName(PeerConnectionFactory.VIDEO_FRAME_EMIT_TRIAL)
.equals(PeerConnectionFactory.TRIAL_ENABLED);
constructionTimeNs = System.nanoTime();
this.cameraThreadHandler = new Handler();
this.callback = callback;
this.events = events;
this.applicationContext = applicationContext;
this.cameraManager = cameraManager;
this.surfaceTextureHelper = surfaceTextureHelper;
this.mediaRecorderSurface = (mediaRecorder != null) ? mediaRecorder.getSurface() : null;
this.cameraId = cameraId;
this.width = width;
this.height = height;
this.framerate = framerate;
start();
}
示例11: isSupported
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
/**
* Checks if API is supported and all cameras have better than legacy support.
*/
public static boolean isSupported(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
try {
String[] cameraIds = cameraManager.getCameraIdList();
for (String id : cameraIds) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(id);
if (characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
== CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
return false;
}
}
// On Android OS pre 4.4.2, a class will not load because of VerifyError if it contains a
// catch statement with an Exception from a newer API, even if the code is never executed.
// https://code.google.com/p/android/issues/detail?id=209129
} catch (/* CameraAccessException */ AndroidException e) {
Logging.e(TAG, "Camera access exception: " + e);
return false;
}
return true;
}
示例12: onCreateView
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
log.i("onCreateView");
View rootView = inflater.inflate(R.layout.fragment_camera, container, false);
mCameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
mHandlerThread = new HandlerThread("CameraFragment_HandlerThread");
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
ttv_camera = (AutoFitTextureView) rootView.findViewById(R.id.ttv_camera);
ttv_camera.setSurfaceTextureListener(this);
tbtn_camera = (ToggleButton) rootView.findViewById(R.id.tbtn_camera);
tbtn_camera.setOnCheckedChangeListener(this);
return rootView;
}
示例13: Camera2Session
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
private Camera2Session(CreateSessionCallback callback, Events events, Context applicationContext,
CameraManager cameraManager, SurfaceTextureHelper surfaceTextureHelper, String cameraId,
int width, int height, int framerate) {
Logging.d(TAG, "Create new camera2 session on camera " + cameraId);
constructionTimeNs = System.nanoTime();
this.cameraThreadHandler = new Handler();
this.callback = callback;
this.events = events;
this.applicationContext = applicationContext;
this.cameraManager = cameraManager;
this.surfaceTextureHelper = surfaceTextureHelper;
this.cameraId = cameraId;
this.width = width;
this.height = height;
this.framerate = framerate;
start();
}
示例14: SimpleCamera2
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
SimpleCamera2(Context context, String deviceName) {
cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
looperThread = new LooperThread();
looperThread.start();
looperThread.waitToStart();
cameraDeviceLock = new Object();
openDoneSignal = new CountDownLatch(1);
cameraDevice = null;
Logging.d(TAG, "Opening simple camera2.");
try {
cameraManager.openCamera(deviceName, new CameraStateCallback(), looperThread.getHandler());
} catch (CameraAccessException e) {
fail("Simple camera2 CameraAccessException: " + e.getMessage());
}
Logging.d(TAG, "Waiting for simple camera2 to open.");
ThreadUtils.awaitUninterruptibly(openDoneSignal);
synchronized (cameraDeviceLock) {
if (!openSucceeded) {
fail("Opening simple camera2 failed.");
}
}
}
示例15: FlashlightController
import android.hardware.camera2.CameraManager; //导入依赖的package包/类
public FlashlightController(Context mContext) {
mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
String cameraId = null;
try {
cameraId = getCameraId();
} catch (Throwable e) {
Log.e(TAG, "Couldn't initialize.", e);
return;
} finally {
mCameraId = cameraId;
}
if (mCameraId != null) {
ensureHandler();
mCameraManager.registerTorchCallback(mTorchCallback, mHandler);
}
}