我写了一个程序使用照相机拍照,但是保存在本地的照片自动旋转了90度,当我从本地读出来上传到网页时也是旋转了90度,我怎么做才能在网页上显示正常呢?这个问题是我很纠结,希望各位高手指点一下……很急……也很感谢各位的帮助……
解决方案 »
- 关于sqlite数据库的错误,我该怎么解决呢,刚接触android开发
- 求问怎么给Fragment中的控件加监听?有几种方法?
- android下opengl多线程渲染
- 请教Android大牛!编写Android Camera相机相关功能程序,出现了一个十分奇怪的问题(关于setType这个过期方法),搞了几天没解决
- 紧急求救android开发遇到问题
- 怎么找到android下对应的设备
- android 简单的问题
- ListView子控件点击,如何获取item的值
- Unable to launch cygpath. Is Cygwin on the path?
- Android中的fragment碎片显示百度地图白屏问题
- GUID存入到字节里面
- 正则代替字符
util.java
public static int getDisplayRotation(Activity activity) {
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
switch (rotation) {
case Surface.ROTATION_0: return 0;
case Surface.ROTATION_90: return 90;
case Surface.ROTATION_180: return 270;
case Surface.ROTATION_270: return 180;
}
return 0;
}
popupwindow.java
public void setOrientation(int orientation) {
switch (orientation) {
case 90:
mRotatePane.setOrientation(RotatePane.LEFT);
break;
case 180:
mRotatePane.setOrientation(RotatePane.DOWN);
break;
case 270:
mRotatePane.setOrientation(RotatePane.RIGHT);
break;
default:
mRotatePane.setOrientation(RotatePane.UP);
break;
}
}
imagemanager.java
public static int getExifOrientation(String filepath) {
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e(TAG, "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
// We only recognize a subset of orientation tag values.
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
} }
}
return degree;
}
其实我个人认为比较可能是sensor驱动有关(某个轴反了),或者是照相驱动。
public class SelectWeiboPicture extends BaseActivity{
private static final String TAG = "SelectWeiboPicture";
private boolean preview;
private Camera camera;
private SurfaceView surfaceView;
public static byte[] picData;
public static File file;
//@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.take_picture_send);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144);
// 设置Surface不维护自己的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(new SurfaceCallback());
}private class SurfaceCallback implements SurfaceHolder.Callback{
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parameters = camera.getParameters();
//"portrait"是竖向
parameters.set("orientation", "portrait");
// 设置照片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 根据屏幕方向设置预览尺寸
parameters.setPreviewSize(480, 320);
parameters.setPictureSize(480, 320);
// 设置保存的图像大小
camera.setParameters(parameters);
// 开始拍照
camera.startPreview();
} public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
// 获取屏幕
Display display = windowManager.getDefaultDisplay();
try {
//获取拍照画面
camera.setPreviewDisplay(surfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
//开始预览
camera.startPreview();
preview = true;
} public void surfaceDestroyed(SurfaceHolder holder) {
if(camera != null){
if(preview){
//停止预览
camera.stopPreview();
}
camera.release();
}
}
} // 通过触摸拍照预览界面结束拍照
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
// 结束拍照
camera.takePicture(null, null, pictureCallback);
return super.onTouchEvent(event);
} Camera.PictureCallback pictureCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
//new SavePictureTask().execute(data);
file = new File(Environment.getExternalStorageDirectory(),"picture.jpg");
try{
//获得文件输出流
FileOutputStream fos = new FileOutputStream(file.getPath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
int bmWidth = bitmap.getWidth();
int bmHeight = bitmap.getHeight();
System.out.println("--------------------------------------");
Matrix matrix = new Matrix();
bitmap = Bitmap.createBitmap(bitmap, 0, 0,bmWidth,
bmHeight,matrix, true);
matrix.postRotate(-90);
//写入文件
fos.write(data);
//清空并关闭文件流
fos.flush();
fos.close();
}catch(Exception ex){
ex.printStackTrace();
}
picData = data;
System.out.println("放入的数据是:" + getIntent().putExtra("file", file));
setResult(1, getIntent());
if(bitmap.isRecycled()==false) //如果没有回收
bitmap.recycle();
camera.stopPreview();
camera = null;
finish();
//camera.startPreview();
}
};
/*class SavePictureTask extends AsyncTask<byte [],String,String>{ @Override
protected String doInBackground(byte[]... params) {
//创建文件
file = new File(Environment.getExternalStorageDirectory(),"picture.jpg");
//如果文件存在就删除掉
if(file.exists()){
file.delete();
}
try{
//获得文件输出流
FileOutputStream fos = new FileOutputStream(file.getPath());
//写入文件
fos.write(params[0]);
//清空并关闭文件流
fos.flush();
fos.close();
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
}*/
}
请大侠再帮忙看看……