- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我实现了一个自定义的 SurfaceView 来绘制相机预览,并在其上完成了所有捕获和手动对焦操作。它在 Pre-Lolipop 设备上运行良好,但问题是它在 Lollipop 设备上崩溃。 发生的最奇怪的事情是,应用程序抛出 ANR,当我单击“确定”时,应用程序从我离开它的地方开始运行。
LogCat 显示“在跳过 ANR 后无法连接到相机错误,我可以轻松连接甚至操纵我的相机”。是因为我使用了已弃用的 Camera API 而不是 Camera2 和 Lollipop 设备吗??
我的代码是这样的:
public class CameraPreview
implements
SurfaceHolder.Callback {
private int cameratype=Camera.CameraInfo.CAMERA_FACING_BACK;
private Camera mCamera = null;
public Camera.Parameters params;
private SurfaceHolder sHolder;
private String TAG="CameraPreview";
public List<Camera.Size> supportedSizes;
public int isCamOpen = 0;
public boolean isSizeSupported = false;
private int previewWidth, previewHeight;
private List<String> mSupportedFlashModes;
private boolean flashon=false;
private final static String MYTAG = "CameraPreview";
private ProgressDialog loading;
public CameraPreview(int width, int height) {
Log.i("campreview", "Width = " + String.valueOf(width));
Log.i("campreview", "Height = " + String.valueOf(height));
previewWidth = width;
previewHeight = height;
}
private int openCamera() {
if (isCamOpen == 1) {
releaseCamera();
}
mCamera = Camera.open(cameratype);
if (mCamera == null) {
return -1;
}
if (TouchActivity.reference.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
mCamera.setDisplayOrientation(90);
} else {
mCamera.setDisplayOrientation(0);
}
params = mCamera.getParameters();
params.setPreviewSize(previewWidth, previewHeight);
try {
mCamera.setParameters(params);
} catch (RuntimeException e) {
e.printStackTrace();
}
mCamera.startPreview();
try {
mCamera.setPreviewDisplay(sHolder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
return -1;
}
isCamOpen = 1;
return isCamOpen;
}
public int isCamOpen() {
return isCamOpen;
}
public void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
isCamOpen = 0;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
sHolder = holder;
isCamOpen = openCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
/**
* Called from PreviewSurfaceView to set touch focus.
*
* @param - Rect - new area for auto focus
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void doTouchFocus(final Rect tfocusRect) {
Log.i(TAG, "TouchFocus");
try {
final List<Camera.Area> focusList = new ArrayList<Camera.Area>();
Camera.Area focusArea = new Camera.Area(tfocusRect, 1000);
focusList.add(focusArea);
Camera.Parameters para = mCamera.getParameters();
para.setFocusAreas(focusList);
para.setMeteringAreas(focusList);
mCamera.setParameters(para);
mCamera.autoFocus(myAutoFocusCallback);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Unable to autofocus");
}
}
/**
* AutoFocus callback
*/
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
if (arg0){
mCamera.cancelAutoFocus();
}
}
};
public void capturePicture(){
mCamera.takePicture(null, null, mPicture);
}
private File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "UltimateCameraGuideApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("Camera Guide", "Required media storage does not exist");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
//DialogHelper.showDialog("Success!", "Your picture has been saved!", TouchActivity.reference.getActivity());
return mediaFile;
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//This One is Just for Getting a File Named after it
loading=new ProgressDialog(BaseImagesContainer.reference);
loading.setMessage("Getting Image Ready");
loading.show();
File pictureFile =getOutputMediaFile();
if (pictureFile == null){
Toast.makeText(TouchActivity.reference.getActivity(), "Image retrieval failed.", Toast.LENGTH_SHORT)
.show();
return;
}
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
if(cameratype==Camera.CameraInfo.CAMERA_FACING_BACK){
bmp=rotateImage(90,bmp);
}else{
bmp=rotateImage(270,bmp);
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,1, stream);
byte[] flippedImageByteArray = stream.toByteArray();
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(flippedImageByteArray);
fos.close();
// Restart the camera preview.
//safeCameraOpenInView(mCameraView);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Uri destination = Uri.fromFile(new File(TouchActivity.reference.getActivity().getCacheDir(), "cropped"));
Uri source = Uri.fromFile(new File(pictureFile.getPath()));
Crop.of(source, destination).withMaxSize(800,800).start(TouchActivity.reference.getActivity());
}
};
public Bitmap rotateImage(int angle, Bitmap bitmapSrc) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
public void switchCamera(){
mCamera.stopPreview();
//NB: if you don't release the current camera before switching, you app will crash
mCamera.release();
//swap the id of the camera to be used
if(cameratype==Camera.CameraInfo.CAMERA_FACING_BACK){
cameratype=Camera.CameraInfo.CAMERA_FACING_FRONT;
}else{
cameratype=Camera.CameraInfo.CAMERA_FACING_BACK;
}
try{
mCamera = Camera.open(cameratype);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(TouchActivity.reference.getActivity(),"Can't Open the Camera",Toast.LENGTH_LONG).show();
}
if (TouchActivity.reference.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
mCamera.setDisplayOrientation(90);
} else {
mCamera.setDisplayOrientation(0);
}
try{
mCamera.setPreviewDisplay(sHolder);
mCamera.startPreview();
}
catch(Exception e){
e.printStackTrace();
}
}
public void switchflash(){
//Do the On Flash for now
if(!flashon){
mSupportedFlashModes = mCamera.getParameters().getSupportedFlashModes();
if (mSupportedFlashModes != null && mSupportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)){
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
mCamera.setParameters(parameters);
}
}else{
//flash on
//do teh off now
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
}
flashon=!flashon;
}
public void stopLoading(){
loading.dismiss();
//DialogHelper.showDialog("Oops!", "Your crop had been cancelled !", TouchActivity.reference.getActivity());
}
最佳答案
根本原因与 Camera Preview appearing really slow in Android 中的相同。我什至在我的 comment 中提到过 ANR那里。
为避免 ANR,您必须将 Camera.open() 卸载到另一个线程。如果您投资在处理程序线程上执行此操作,如所述here ,您的 onPictureTaken() 回调也不会导致 ANR。
您需要将此回调的某些部分包装在 runOnUiThread()
中,但这肯定会有所返回。
关于android - SurfaceView 相机预览代码在 Pre Lollipop 上运行良好但在 Lollipop 设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658402/
嗨,我正在考虑开发一种文件传输程序,想知道我是否想要尽可能好的加密,我应该使用什么? 我会用 C# 开发它,所以我可以访问 .net 库 :P在我的 usb 上有一个证书来访问服务器是没有问题的,如果
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
我已经能够使用 RTCPeerConnection.getStats() API 获得 WebRTC 音频调用的各种统计信息(抖动、RTT、丢包等)。 我需要将整体通话质量评为优秀、良好、一般或差。
基本问题: 如果我正在讲述/修改数据,我应该通过索引硬编码索引访问文件的元素,即 targetFile.getElement(5);通过硬编码标识符(内部翻译成索引),即 target.getElem
在 Linux 上,我想知道要调用什么“C”API 来获取每个 CPU 的统计信息。 我知道并且可以从我的应用程序中读取 /proc/loadavg,但这是系统范围的负载平均值,而不是每个 CPU 的
在客户端浏览器中使用 fetch api,GET 或 POST 没有问题,但 fetch 和 DELETE 有问题。它似乎将 DELETE 请求方法更改为 OPTIONS。 大多数研究表明是一个cor
我是一名优秀的程序员,十分优秀!