控制相机拍摄的人像照片不旋转的最终图像 [英] Controlling the camera to take pictures in portrait doesn't rotate the final images
问题描述
我试图控制Android相机拍照在肖像的应用程序,但是当我保存的图片,它的景观。我旋转的图像90的成绩与<一个href="http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29">setCameraDisplayOrientation()的方法,但不工作
然后我发现这个<一href="http://stackoverflow.com/questions/11023696/setrotation90-to-take-picture-in-portrait-mode-does-not-work-on-samsung-device">post但TAG_ORIENTATION为0(未定义)。如果我抓住这个值,并应用旋转值,也不管用。
我可以如何采取人像照片,并保存它具有良好的方向?
/ **初始化正面/背面摄像头* /
私人布尔initPhotoCamera(){
尝试 {
摄像头= getCameraInstance(selected_camera);
Camera.Parameters参数= camera.getParameters();
// parameters.set previewSize(width_video,height_video);
// parameters.set(方向,肖像);
// parameters.set(旋转,1);
// camera.setParameters(参数);
checkCameraFlash(参数);
// camera.setDisplayOrientation(0);
setCameraDisplayOrientation(selected_camera,摄像头);
。surface_view.getHolder()setFixedSize(width_video,height_video);
LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(width_video,height_video);
surface_view.setLayoutParams(LP);
camera.lock();
surface_holder = surface_view.getHolder();
surface_holder.addCallback(本);
surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
集previewCamera();
}赶上(例外五){
Log.v(RecordVideo,无法初始化相机);
返回false;
}
返回true;
}
公共无效setCameraDisplayOrientation(INT cameraId,相机摄像头){
Camera.CameraInfo信息=新Camera.CameraInfo();
Camera.getCameraInfo(cameraId,资讯);
INT旋转= getWindowManager()getDefaultDisplay()getRotation()。
INT度= 0;
开关(旋转){
案例Surface.ROTATION_0:度= 0;打破;
案例Surface.ROTATION_90:度= 90;打破;
案例Surface.ROTATION_180:度= 180;打破;
案例Surface.ROTATION_270:度= 270;打破;
}
INT结果;
如果(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
结果=(info.orientation +度)%360;
结果=(360 - 结果)%360; //补偿镜
}其他{//背向
结果=(info.orientation - 度+ 360)%360;
}
camera.setDisplayOrientation(结果);
}
公共静态位图旋转(位图位图,INT度){
INT W = bitmap.getWidth();
INT H = bitmap.getHeight();
矩阵MTX =新的Matrix();
// mtx.postRotate(度);
mtx.setRotate(度);
返回Bitmap.createBitmap(位图,0,0,W,H,MTX,真);
}
@覆盖
公共无效onPictureTaken(byte []的数据,摄像头摄像头){
字符串的timeStamp = Calendar.getInstance()的getTime()的toString()。
output_file_name中= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+文件分割符+的timeStamp +.JPEG;
文件PictureFile的=新的文件(output_file_name中);
如果(pictureFile.exists()){
pictureFile.delete();
}
尝试 {
FileOutputStream中FOS =新的FileOutputStream(PictureFile的);
fos.write(数据);
位图realImage = BitmapFactory.de codeFILE(output_file_name中);
ExifInterface EXIF =新ExifInterface(pictureFile.toString());
Log.d(EXIF价值,exif.getAttribute(ExifInterface.TAG_ORIENTATION));
如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(6)){
realImage =旋转(realImage,90);
}否则,如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(8)){
realImage =旋转(realImage,270);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(3)){
realImage =旋转(realImage,180);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(0)){
realImage =旋转(realImage,45);
}
布尔博= realImage.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
fos.close();
Log.d(信息,微博+);
}赶上(FileNotFoundException异常E){
Log.d(信息,找不到文件:+ e.getMessage());
}赶上(IOException异常E){
Log.d(TAG,访问文件时出错:+ e.getMessage());
}
}
现在的问题是,当我救我的形象没有做好。
@覆盖
公共无效onPictureTaken(byte []的数据,摄像头摄像头){
字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date())。
output_file_name中= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+文件分割符+的timeStamp +.JPEG;
文件PictureFile的=新的文件(output_file_name中);
如果(pictureFile.exists()){
pictureFile.delete();
}
尝试 {
FileOutputStream中FOS =新的FileOutputStream(PictureFile的);
位图realImage = BitmapFactory.de codeByteArray(数据,0,data.length);
ExifInterface EXIF =新ExifInterface(pictureFile.toString());
Log.d(EXIF价值,exif.getAttribute(ExifInterface.TAG_ORIENTATION));
如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(6)){
realImage =旋转(realImage,90);
}否则,如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(8)){
realImage =旋转(realImage,270);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(3)){
realImage =旋转(realImage,180);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(0)){
realImage =旋转(realImage,90);
}
布尔博= realImage.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
fos.close();
((ImageView的)findViewById(R.id.imageview))setImageBitmap(realImage)。
Log.d(信息,微博+);
}赶上(FileNotFoundException异常E){
Log.d(信息,找不到文件:+ e.getMessage());
}赶上(IOException异常E){
Log.d(TAG,访问文件时出错:+ e.getMessage());
}
}
I'm trying to controlling the Android camera to take pictures in a portrait app, but when I save the picture, it's in landscape. I've rotated the image 90 grades with setCameraDisplayOrientation() method, but doesn't work.
Then I've found this post but the TAG_ORIENTATION is 0 (undefined). If I catch this value and apply a rotation value, doesn't work either.
How I can take a photo in portrait and save it with a good orientation?
/** Initializes the back/front camera */
private boolean initPhotoCamera() {
try {
camera = getCameraInstance(selected_camera);
Camera.Parameters parameters = camera.getParameters();
// parameters.setPreviewSize(width_video, height_video);
// parameters.set("orientation", "portrait");
// parameters.set("rotation", 1);
// camera.setParameters(parameters);
checkCameraFlash(parameters);
// camera.setDisplayOrientation( 0);
setCameraDisplayOrientation(selected_camera, camera);
surface_view.getHolder().setFixedSize(width_video, height_video);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width_video, height_video);
surface_view.setLayoutParams(lp);
camera.lock();
surface_holder = surface_view.getHolder();
surface_holder.addCallback(this);
surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setPreviewCamera();
} catch (Exception e) {
Log.v("RecordVideo", "Could not initialize the Camera");
return false;
}
return true;
}
public void setCameraDisplayOrientation(int cameraId, Camera camera) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = 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;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
// mtx.postRotate(degree);
mtx.setRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
String timeStamp = Calendar.getInstance().getTime().toString();
output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".jpeg";
File pictureFile = new File(output_file_name);
if (pictureFile.exists()) {
pictureFile.delete();
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
Bitmap realImage = BitmapFactory.decodeFile(output_file_name);
ExifInterface exif=new ExifInterface(pictureFile.toString());
Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
realImage= rotate(realImage, 90);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
realImage= rotate(realImage, 270);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
realImage= rotate(realImage, 180);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
realImage= rotate(realImage, 45);
}
boolean bo = realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
Log.d("Info", bo + "");
} catch (FileNotFoundException e) {
Log.d("Info", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("TAG", "Error accessing file: " + e.getMessage());
}
}
The problem is when I saved the image I didn't do well.
@Override
public void onPictureTaken(byte[] data, Camera camera) {
String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss").format( new Date( ));
output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".jpeg";
File pictureFile = new File(output_file_name);
if (pictureFile.exists()) {
pictureFile.delete();
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
ExifInterface exif=new ExifInterface(pictureFile.toString());
Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
realImage= rotate(realImage, 90);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
realImage= rotate(realImage, 270);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
realImage= rotate(realImage, 180);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
realImage= rotate(realImage, 90);
}
boolean bo = realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
((ImageView) findViewById(R.id.imageview)).setImageBitmap(realImage);
Log.d("Info", bo + "");
} catch (FileNotFoundException e) {
Log.d("Info", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("TAG", "Error accessing file: " + e.getMessage());
}
}
这篇关于控制相机拍摄的人像照片不旋转的最终图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!