我正在使用Surfaceholder和相机来捕捉图像,但当我在HTC Desire S上执行此操作时,捕获的图像看起来像这样。这不是预览视图中的样子。
它适用于我测试的其他两部手机,因此应该是设备相关的问题,但我无法弄清楚是什么。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_camera_overlay);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.overlay, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}});
}
...
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] byteArray, Camera arg1) {
//Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String date = dateFormat.format(new Date());
String _path = Environment.getExternalStorageDirectory() + File.separator + "xxx" + File.separator + "Livboj" + date + ".jpg";
File file = new File( _path );
Uri uriTarget = Uri.fromFile( file );
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(byteArray);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(CameraOverlayActivity.this,"Image saved: " + uriTarget.toString(),Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height);
parameters.setPreviewSize(size.width, size.height);
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
camera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
}
if(display.getRotation() == Surface.ROTATION_180)
{
}
if(display.getRotation() == Surface.ROTATION_270)
{
camera.setDisplayOrientation(180);
}
camera.setParameters(parameters);
previewCamera();
}
private Camera.Size getBestPreviewSize(int width, int height)
{
Camera.Size result=null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
} else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return result;
}
public void previewCamera()
{
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}
catch(Exception e)
{
Log.d("Camera", "Cannot start preview", e);
}
}
我不知道问题出在保存或捕获中。有谁遇到过同样的问题?提前谢谢!