我正在尝试获取相机预览并将其中一部分用于在ImageView上显示。此代码在模拟器上正常工作但在设备上崩溃并出现以下错误:
A / libc(2659):致命信号11(SIGSEGV)位于0x42e612a9(代码= 2),线程 2659
我查了很多与SIGSEGV错误有关的问题,但我找不到任何解决这个问题的方法。我的设备和模拟器是Android 4.1.2
Size sizeOrgnl;
Rect newRect;
ByteArrayOutputStream newStream;
Bitmap newPicture;
ImageView imgView;
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int w, int h) {
camera=Camera.open();
if(camera!=null)
{ try
{
List<Size> cmszs=camera.getParameters().getSupportedPictureSizes();
for(int i=0;i<cmszs.size();i++)
{
if(cmszs.get(i).width<500&&(cmszs.get(i).width>400))
{camera.getParameters().setPictureSize(cmszs.get(i).width, cmszs.get(i).height);}
}
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(arg0);
camera.stopPreview();
camera.startPreview();
Thread.sleep(500);
newStream=new ByteArrayOutputStream();
imgView=(ImageView)findViewById(R.id.imageView1);
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if(sizeOrgnl==null)
{sizeOrgnl=camera.getParameters().getPictureSize();
newRect=new Rect(sizeOrgnl.width/2-20,sizeOrgnl.height/2-20,sizeOrgnl.width/2+20,sizeOrgnl.height/2+20);
}
YuvImage img = new YuvImage(data, ImageFormat.NV21, sizeOrgnl.width, sizeOrgnl.height, null);
newStream=new ByteArrayOutputStream();
img.compressToJpeg(newRect, 90, newStream);
newPicture = BitmapFactory.decodeByteArray(newStream.toByteArray() , 0, newStream.size());
imgView.setImageBitmap(newPicture);
}
});
} catch(Exception e){}
}
此行发生错误(但仅限于设备上):
img.compressToJpeg(newRect, 90, newStream);
感谢。