为什么图像被保存在横向模式? [英] Why image is being saved in Landscape Mode?
本文介绍了为什么图像被保存在横向模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的code一切是怎么回事很好,但点击图片在给定的目录被保存在横向模式。我已经尽了全力。请帮助和建议我来解决这个问题。
完整code在下面给出与打印屏幕。
或从这里下载
https://www.dropbox.com/sh/vp6ohly4zb5vmvq/AACOyS8PvsRxFGBP9zjlVWhza? DL = 0
保存在SD卡的图像,即使在人像拍摄的它出现在横向
公共类AndroidCamera扩展活动实现SurfaceHolder.Callback { 摄像头摄像头; SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
布尔previewing = FALSE;
LayoutInflater controlInflater = NULL; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//camera.enableShutterSound(true);
。getWindow()和setFormat(PixelFormat.UNKNOWN);
surfaceView =(SurfaceView)findViewById(R.id.camera preVIEW);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(本);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); controlInflater = LayoutInflater.from(getBaseContext());
查看viewControl = controlInflater.inflate(R.layout.control,NULL);
的LayoutParams layoutParamsControl
=新的LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl,layoutParamsControl); 按钮buttonTakePicture =(按钮)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(新Button.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
camera.takePicture(myShutterCallback,
myPictureCallback_RAW,myPictureCallback_JPG);
}});
} ShutterCallback myShutterCallback =新ShutterCallback(){ @覆盖
公共无效onShutter(){
// TODO自动生成方法存根 }}; PictureCallback myPictureCallback_RAW =新PictureCallback(){ @覆盖
公共无效onPictureTaken(字节[]为arg0,ARG1相机){
// TODO自动生成方法存根 }}; PictureCallback myPictureCallback_JPG =新PictureCallback(){ @覆盖
公共无效onPictureTaken(字节[]为arg0,ARG1相机){
// TODO自动生成方法存根 位图bitmapPicture
= BitmapFactory.de codeByteArray的(为arg0,0,arg0.length);
。字符串根= Environment.getExternalStorageDirectory()的toString();
文件MYDIR =新的文件(根+/ cam_Api);
myDir.mkdirs(); FileOutputStream中outStream = NULL;
尝试{
outStream =新的FileOutputStream(的String.format(/ SD卡/ cam_Api / cam_Ap1_%D.JPG,System.currentTimeMillis的())); outStream.write(为arg0);
outStream.close();
} 赶上(FileNotFoundException异常五){
e.printStackTrace();
} 赶上(IOException异常五){
e.printStackTrace();
} 最后{
} Toast.makeText(getApplicationContext(),图片已保存,Toast.LENGTH_LONG).show();
refreshCamera();//文件文件=新的文件(Environment.getExternalStorageDirectory()/ cam_Api / MyPhoto2.jpg); }}; 公共无效refreshCamera(){
如果(surfaceHolder.getSurface()== NULL){
返回;
} 尝试{
camera.stop preVIEW();
} 赶上(例外五){
} 尝试{
camera.set previewDisplay(surfaceHolder);
camera.start preVIEW();
}
赶上(例外五){
}
} @覆盖
公共无效surfaceChanged(SurfaceHolder架,INT格式,宽度INT,
INT高度){
// TODO自动生成方法存根
如果(previewing){
camera.stop preVIEW();
previewing = FALSE;
} 如果(相机!= NULL){
尝试{
camera.set previewDisplay(surfaceHolder);
camera.start preVIEW();
previewing = TRUE;
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
} @覆盖
公共无效surfaceCreated(SurfaceHolder持有人){
// TODO自动生成方法存根 相机= Camera.open();
camera.setDisplayOrientation(90);
} @覆盖
公共无效surfaceDestroyed(SurfaceHolder持有人){
// TODO自动生成方法存根
camera.stop preVIEW();
camera.release();
摄像头= NULL;
previewing = FALSE;
}
}
解决方案
您需要旋转图像,然后保存。
公共静态位图rotateImage(位图bitmapSrc){
字模=新的Matrix();
matrix.postRotate(CameraConfigurationUtils previewRotation);
返回Bitmap.createBitmap(bitmapSrc,0,0,
bitmapSrc.getWidth(),bitmapSrc.getHeight(),矩阵,真);
}
要获取旋转:
私有静态诠释setCameraDisplayOrientation(上下文mContext,android.hardware.Camera.CameraInfo资讯){
INT旋转=((CameraActivity)mContext).getWindowManager()。getDefaultDisplay()
.getRotation();
INT度= 0;
开关(旋转){
案例Surface.ROTATION_0:
度= 0;
打破;
案例Surface.ROTATION_90:
度= 90;
打破;
案例Surface.ROTATION_180:
度= 180;
打破;
案例Surface.ROTATION_270:
度= 270;
打破;
} 如果(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
previewRotation =(info.orientation +度)360%;
previewRotation =(360 - previewRotation)360%; //补偿镜
}其他{//后置
previewRotation =(info.orientation - 度+ 360)%360;
}
返回previewRotation;
}
In my code all is going well but clicked Image is being saved in Landscape Mode in given Directory. I have tried my best. Please help and suggest me to resolve this issue.
The complete Code is given below with Print screen. or download from here
https://www.dropbox.com/sh/vp6ohly4zb5vmvq/AACOyS8PvsRxFGBP9zjlVWhza?dl=0
Image saved in SDcard even captured in portrait it appear in landscape
public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//camera.enableShutterSound(true);
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.control, 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);
}});
}
ShutterCallback myShutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/cam_Api");
myDir.mkdirs();
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format("/sdcard/cam_Api/cam_Ap1_%d.jpg", System.currentTimeMillis()));
outStream.write(arg0);
outStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
}
Toast.makeText(getApplicationContext(), "Picture Saved", Toast.LENGTH_LONG).show();
refreshCamera();
// File file = new File(Environment.getExternalStorageDirectory(),"/cam_Api/MyPhoto2.jpg");
}};
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {
return;
}
try {
camera.stopPreview();
}
catch (Exception e) {
}
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}
catch (Exception e) {
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
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();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
camera.setDisplayOrientation(90);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
解决方案
you need to rotate the image and then save it.
public static Bitmap rotateImage(Bitmap bitmapSrc) {
Matrix matrix = new Matrix();
matrix.postRotate(CameraConfigurationUtils.previewRotation);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
To get the rotation:
private static int setCameraDisplayOrientation(Context mContext, android.hardware.Camera.CameraInfo info) {
int rotation = ((CameraActivity) mContext).getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
previewRotation = (info.orientation + degrees) % 360;
previewRotation = (360 - previewRotation) % 360; // compensate the mirror
} else { // back-facing
previewRotation = (info.orientation - degrees + 360) % 360;
}
return previewRotation;
}
这篇关于为什么图像被保存在横向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文