我想根据Android中的设备方向设置相机方向,但似乎没有任何效果.我尝试旋转Surface以及相机参数,但是纵向模式下的相机预览总是颠倒过来.我需要顺时针旋转90度才能使其正确.这是我现在使用的代码,仅适用于横向模式.
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
initCamera();
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size …Run Code Online (Sandbox Code Playgroud) 我有一个纵向模式的相机应用程序,它从前端和后端相机拍摄照片.我将图像保存在我的SD卡中并尝试找到相应的exif值,它总是给出0.但是我得到了预期的exif方向存储在设备中的其他图像的值(如下载的图片).
我怎样才能解决这个问题 ?谁能帮我吗 ?
这是用于保存图片和查找方向的代码
PictureCallback myPictureCallback_JPG = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
try {
File APP_FILE_PATH = new File(Environment.getExternalStorageDirectory()
.getPath() + "/Myapp/");
if (!APP_FILE_PATH.exists()) {
APP_FILE_PATH.mkdirs();
}
File file = new File(APP_FILE_PATH, "image.jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(arg0);
fos.close();
imageFileUri=Uri.fromfile(file); getApplicationContext().getContentResolver().notifyChange(
imageFileUri, null);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
ExifInterface exif = new ExifInterface(file.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
} catch (Exception e) {
}
}
};
Run Code Online (Sandbox Code Playgroud)
以下是surphace创建和更改函数的代码 …
好的,我有一个扩展SurfaceView和覆盖的类
surfaceChanged - 只需调用startPreview
surfaceCreated - 打开相机,编辑params*,设置surfaceHolder
surfaceDestroyed - 调用stopPreview,释放相机
这一切都很有效,因为当方向是肖像:
来自surfaceCreated*
m_camera = Camera.open();
Camera.Parameters p = m_camera.getParameters();
if (getResources().getConfiguration().orientation !=
Configuration.ORIENTATION_LANDSCAPE)
{
p.set("orientation", "portrait");
// CameraApi is a wrapper to check for backwards compatibility
if (CameraApi.isSetRotationSupported())
{
CameraApi.setRotation(p, 90);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每次方向改变时,它都会调用Camera.open()......正如您所知,这是一项非常昂贵的操作,导致转换不是那么顺利.
当我强制定位到横向时,预览很棒.创建只调用一次,因为预览是横向的,相机始终是用户看到的.但是,我需要一种方法来设置纵向拍摄时实际拍摄的照片的方向.当我强制横向渲染时,表面永远不会被重新创建,并且当相机以纵向方式保持时,参数永远不会被设置.
那么如何才能完成以下任一项(专门)?
当方向发生变化时,保持 onDestroy和onCreate之间的m_camera,以便过渡平滑
强制风景和检测方向改变另一种方式...旋转最终的snaped图片,如果保持纵向.
此外,如果我离开基地,有人能指出我更好的方向吗?谢谢.
android smartphone orientation android-sdk-2.1 android-camera
在纵向模式下,图像看起来垂直拉伸,在横向模式下,它看起来水平拉伸.
虽然捕获图像后显示的大小合适.
如何解决这个问题?
我点击按钮打开相机应用程序.并在下一个活动中显示捕获的照片.但捕获的照片旋转了90度.当我捕捉到图像后在视图中显示图像时,它的方向始终是横向的.为什么在纵向模式下拍摄照片时,照片不会以纵向显示?
点击按钮:
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(APP_DIR+"/latest.png")));
startActivityForResult(i, CAPTURE_PHOTO_CONSTANT);
Run Code Online (Sandbox Code Playgroud)
在onActvityresult里面:
bmp = BitmapFactory.decodeFile(APP_DIR+"/latest.png");
startActivity(new Intent(this, DisplayActivity.class));
Run Code Online (Sandbox Code Playgroud)
显示拍摄的照片:
photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp));
Run Code Online (Sandbox Code Playgroud) 这个问题类似于这里,这里,这里,这里和这里的帖子,但我被困住了,花了好几个小时试图解决它.
我有一个摄像机预览(现在总是以正确的方向显示),但是当我点击记录(mediaRecorder.start();)时,视频方向会改变.我尝试过使用setOrientationHint,但它似乎没有什么区别(如下面代码中的注释所标记).
它会影响我的测试设备(Galaxy和Jelly Bean上的Xperia).我该怎么做才能解决这个问题?
这是我的代码:
XML
<RelativeLayout android:id="@+id/surface_camera"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/videoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button
android:id="@+id/mybutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="REC"
android:textSize="12dp"/>
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Java的
package hockeyj.androidlisttesting;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button; …Run Code Online (Sandbox Code Playgroud)