从ImageView的显示相机拍摄的图片总是水平的Android [英] Picture taken from camera displayed in ImageView is always horizontal Android
问题描述
我用下面的code拍摄照片,然后使用ImageView的在屏幕上显示它给用户。我想使用全屏图片(像snapchat)。然而,实际情况总是显示这样水平的白色屏幕其他地方:
这是我的布局:
<的FrameLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。><的FrameLayout
机器人:ID =@ + ID / camera_view
机器人:layout_width =match_parent
机器人:layout_height =match_parent>< /&的FrameLayout GT;
< ImageView的
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:知名度=水涨船高
机器人:ID =@ + ID / picturedisplay/><按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / imgClose
机器人:layout_gravity =右|底
机器人:文字=翻转凸轮
机器人:填充=20dp/>
<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /抓拍
机器人:文字=捕捉
机器人:layout_gravity =中心|底
机器人:填充=20dp/>
<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=闪光
机器人:ID =@ + ID / imgOpen
机器人:layout_gravity =左|底
机器人:填充=20dp/>
和code:
公共类CameraScreen延伸活动{
私人相机mCamera = NULL;
私人是SessionManager会议;
私人字符串rand_img;
私人ImageView的preview_pic;
私人位图位图;
私人相机preVIEW mCameraView = NULL;
私人字节[]的照片;
静态最终诠释CAM_REQUEST = 1;
私人RandomString randomString =新RandomString(10);
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_camera_screen);
会话=新SessionManager(中间getApplicationContext());
尝试{
mCamera = Camera.open(); //你可以使用开放式(INT)使用不同的相机
}赶上(例外五){
Log.d(ERROR,无法取得摄像头:+ e.getMessage());
}
如果(mCamera!= NULL){
mCameraView =新相机preVIEW(这一点,mCamera); //创建一个SurfaceView显示相机数据
的FrameLayout camera_view =(的FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView); //将SurfaceView添加到布局
} // BTN关闭应用程序
按钮imgClose =(按钮)findViewById(R.id.imgClose);
imgClose.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
System.exit(0);
}
});
// BTN关闭应用程序
按钮注销=(按钮)findViewById(R.id.imgOpen);
logout.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
session.logOut();
意图=新意图(CameraScreen.this,MainActivity.class);
startActivity(一);
完();
}
});
扣四合=(按钮)findViewById(R.id.snap);
snap.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
mCamera.takePicture(shutterCallback,rawCallback,jpegCallback);
}
});
}@覆盖
保护无效的onPause(){
super.onPause();
如果(mCamera!= NULL){
mCamera.set previewCallback(NULL);
。mCameraView.getHolder()removeCallback(mCameraView);
mCamera.release();
}
}
@覆盖
公共无效onResume(){
super.onResume(); //获取相机实例作为活动达到完整的用户重点
如果(mCamera == NULL){
initializeCamera(); //本地方法来处理摄像机初始化
}
}保护无效initializeCamera(){
//获取Camera对象的实例
尝试{
mCamera = Camera.open(); //你可以使用开放式(INT)使用不同的相机
}赶上(例外五){
Log.d(ERROR,无法取得摄像头:+ e.getMessage());
}
如果(mCamera!= NULL){
mCameraView =新相机preVIEW(这一点,mCamera); //创建一个SurfaceView显示相机数据
的FrameLayout camera_view =(的FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView); //将SurfaceView添加到布局
}
}
私人文件的GetFile(){
文件SD卡= Environment.getExternalStorageDirectory();
文件夹=新的文件(sdCard.getAbsolutePath()+/ city_life_pic);
如果(!folder.exists()){
folder.mkdir();
}
rand_img = randomString.nextString()+.JPG;
文件图像=新的文件(文件夹,rand_img);
返回形象;
}Camera.ShutterCallback shutterCallback =新Camera.ShutterCallback(){
公共无效onShutter(){
Log.d(关于快门,onShutter'd);
}
};Camera.PictureCallback rawCallback =新Camera.PictureCallback(){
公共无效onPictureTaken(字节[]数据,相机摄像头){
Log.d(ON图像的RAW,onPictureTaken - 原);
}
};Camera.PictureCallback jpegCallback =新Camera.PictureCallback(){
公共无效onPictureTaken(字节[]数据,相机摄像头){
照片=数据;
新SaveImageTask()执行(数据);
Log.d(照片JPEG,onPictureTaken - JPEG);
}
};私有类SaveImageTask扩展的AsyncTask<字节[],虚空,虚空> { @覆盖
保护无效doInBackground(字节[] ...数据){
FileOutputStream中outStream = NULL; //写入SD卡
尝试{
位=去codeByteArray的(照片,0,photo.length);
文件不过outFile =的GetFile();
outStream =新的FileOutputStream(不过outFile);
outStream.write(数据[0]); ExifInterface EXIF =新ExifInterface(outFile.toString()); Log.d(EXIF价值,exif.getAttribute(ExifInterface.TAG_ORIENTATION));
如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(6)){
位图=移位(位图,90);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(8)){
位=旋转(位图,270);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(3)){
位=旋转(位图,180);
}否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(0)){
位图=移位(位图,90);
} 布尔博= bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,outStream);
outStream.flush();
outStream.close();
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
} {最后
}
返回null;
} @覆盖
保护无效onPostExecute(虚空避免){
的FrameLayout camera_view =(的FrameLayout)findViewById(R.id.camera_view);
preview_pic =(ImageView的)findViewById(R.id.picturedisplay);
camera_view.setVisibility(View.GONE);
preview_pic.setVisibility(View.VISIBLE);
preview_pic.setImageBitmap(位图);
}
}
公共静态位图移位(位图位图,诠释度){
INT W = bitmap.getWidth();
INT H = bitmap.getHeight(); 矩阵MTX =新的Matrix();
// mtx.postRotate(度);
mtx.setRotate(度); 返回Bitmap.createBitmap(位图,0,0,W,H,MTX,真);
}
}
编辑循环code:
开关(旋转)
{
案例Surface.ROTATION_0:
度= 0;
打破;
案例Surface.ROTATION_90:
度= 90;
打破;
案例Surface.ROTATION_180:
度= 180;
打破;
案例Surface.ROTATION_270:
度= 270;
打破;
}
如果(currentCamInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
//切换摄像头后置摄像头
mCamera = Camera.open(camBackId);
结果=(currentCamInfo.orientation +度)360%;
结果=(360 - 结果)%360; //补偿镜
}其他{
//切换摄像头前置摄像头
mCamera = Camera.open(camFrontId);
结果=(currentCamInfo.orientation - 度+ 360)%360; }
如果(mCamera!= NULL){
mCamera.setDisplayOrientation(结果);
//旋转摄像头
mCameraView =新相机preVIEW(这一点,mCamera); //创建一个SurfaceView显示相机数据
camera_view.addView(mCameraView); //将SurfaceView添加到布局
Camera.Parameters p值= mCamera.getParameters();
p.setRotation(90);
mCamera.setParameters(P);
}
尝试添加mCamera.setDisplayOrientation(90)你initializeCamera()方法。您还可以在此基础上code计算正确的方向设置。
公共静态INT getCameraDisplayOrientation(INT cameraId,android.hardware.Camera相机){
android.hardware.Camera.CameraInfo信息=
新android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId,资讯);
INT旋转= Session.currentActivity.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;
}
返回结果;
}
I am using the following code to take a picture and then display it on screen to the user using an ImageView. I want the picture to be displayed fullscreen (like on snapchat). However, the picture is always displayed like this horizontally with a white screen everywhere else:
This is my layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="@+id/picturedisplay"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgClose"
android:layout_gravity="right|bottom"
android:text="Flip Cam"
android:padding="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/snap"
android:text="Capture"
android:layout_gravity="center|bottom"
android:padding="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flash"
android:id="@+id/imgOpen"
android:layout_gravity="left|bottom"
android:padding="20dp"/>
and the code:
public class CameraScreen extends Activity {
private Camera mCamera = null;
private SessionManager session;
private String rand_img;
private ImageView preview_pic;
private Bitmap bitmap;
private CameraPreview mCameraView = null;
private byte[] photo;
static final int CAM_REQUEST = 1;
private RandomString randomString = new RandomString(10);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_screen);
session = new SessionManager(getApplicationContext());
try {
mCamera = Camera.open();//you can use open(int) to use different cameras
} catch (Exception e) {
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if (mCamera != null) {
mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout) findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
//btn to close the application
Button imgClose = (Button) findViewById(R.id.imgClose);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.exit(0);
}
});
//btn to close the application
Button logout = (Button) findViewById(R.id.imgOpen);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
session.logOut();
Intent a = new Intent(CameraScreen.this, MainActivity.class);
startActivity(a);
finish();
}
});
Button snap = (Button) findViewById(R.id.snap);
snap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCameraView.getHolder().removeCallback(mCameraView);
mCamera.release();
}
}
@Override
public void onResume() {
super.onResume();
// Get the Camera instance as the activity achieves full user focus
if (mCamera == null) {
initializeCamera(); // Local method to handle camera initialization
}
}
protected void initializeCamera(){
// Get an instance of Camera Object
try{
mCamera = Camera.open();//you can use open(int) to use different cameras
} catch (Exception e){
Log.d("ERROR", "Failed to get camera: " + e.getMessage());
}
if(mCamera != null) {
mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
camera_view.addView(mCameraView);//add the SurfaceView to the layout
}
}
private File getFile() {
File sdCard = Environment.getExternalStorageDirectory();
File folder = new File(sdCard.getAbsolutePath() +"/city_life_pic");
if (!folder.exists()) {
folder.mkdir();
}
rand_img = randomString.nextString() + ".jpg";
File image = new File(folder,rand_img);
return image;
}
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.d("ON SHUTTER", "onShutter'd");
}
};
Camera.PictureCallback rawCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("ON PICTURE RAW", "onPictureTaken - raw");
}
};
Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
photo = data;
new SaveImageTask().execute(data);
Log.d("ON PICTURE JPEG", "onPictureTaken - jpeg");
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
// Write to SD Card
try {
bitmap = decodeByteArray(photo, 0, photo.length);
File outFile = getFile();
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
ExifInterface exif=new ExifInterface(outFile.toString());
Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
bitmap= rotate(bitmap, 90);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
bitmap= rotate(bitmap, 270);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
bitmap= rotate(bitmap, 180);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
bitmap= rotate(bitmap, 90);
}
boolean bo = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
preview_pic = (ImageView) findViewById(R.id.picturedisplay);
camera_view.setVisibility(View.GONE);
preview_pic.setVisibility(View.VISIBLE);
preview_pic.setImageBitmap(bitmap);
}
}
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);
}
}
EDIT ROTATION CODE:
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 (currentCamInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
//switch camera to back camera
mCamera = Camera.open(camBackId);
result = (currentCamInfo.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else {
//switch camera to front camera
mCamera = Camera.open(camFrontId);
result = (currentCamInfo.orientation - degrees + 360) % 360;
}
if (mCamera != null) {
mCamera.setDisplayOrientation(result);
//rotate camera
mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
camera_view.addView(mCameraView);//add the SurfaceView to the layout
Camera.Parameters p = mCamera.getParameters();
p.setRotation(90);
mCamera.setParameters(p);
}
Try adding mCamera.setDisplayOrientation(90) to your initializeCamera() method. You can also calculate the right orientation settings based on this code.
public static int getCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = Session.currentActivity.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;
}
return result;
}
这篇关于从ImageView的显示相机拍摄的图片总是水平的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!