我使用以下代码捕获图像。其中我使用Surfaceview
。捕获的图像保存在我创建的android Sdcard文件夹中,但是当我在GridView
中显示这些图像时,图像方向不同。如果我使用Intent (ACTION_IMAGE_CAPTURE)
使用摄像头,那么输出图像已被很好地定义。任何一个可以帮助我为什么使用Surface视图的相机图像与使用Intent捕捉的图像不同(ACTION_IMAGE_CAPTURE)
这里我有延伸Activity类工具SurfaceHolder.Callback
public class CameraView extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "CameraVeiw";
private Camera camera;
boolean previewing = false;
SurfaceHolder surfaceHolder;
private File file = null;
private static String mFileName = null;
private long name;
final int RESULT_SAVEIMAGE = 0;
private int w;
private int h;
public static byte[] data = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
}
我用SurfaceView
捕捉图像
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceView.getWindowVisibility();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.setFixedSize(300, 300); //hard coded
Button button2 = (Button) findViewById(R.id.picbutton);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
camera.takePicture(new CustomShutter(), null,
new CustomPictureCallback());
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
camera.setDisplayOrientation(90); }
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
class CustomPictureCallback implements Camera.PictureCallback {
private Bitmap bitmap;
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
String path = "/sdcard/MonsterApp/Photos/" + data+".jpg";
outStream = new FileOutputStream(String.format(path));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
camera.startPreview();
Log.i("Information", "Helllllooooo"); } }
class CustomShutter implements Camera.ShutterCallback {
public void onShutter() {
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(camera != null) {
camera.release();
}
}
}
很抱歉,但我在GridView中使用Android默认的相机,并从SD卡中的图片形象定位是为了很好......但是当我使用Surfacview捕捉图像比问题发生(SurfaceView捕获图像在potrait模式,但显示图像在风景模式..) – rajeev
是啊我使用此代码.....但此代码设置相机在横向模式下不使用此代码我的相机处于potrait模式... 。但主要prblm是在横向模式下显示的图像.... – rajeev
好吧我想告诉你一件事,当您使用相机不同的设备具有不同的相机方向在纵向和横向模式。所以你无法处理它的每一个设备,如果你想设置你的旋转肖像模式,那么我张贴一些代码尝试告诉我是前置摄像头还是后退。 –