2017-01-22 47 views
0

即时通讯使用Android Studio。当我执行代码拍摄照片时,预览始终以横向模式显示,但图像以纵向保存。
如何让预览以纵向模式显示?Android。 Mashmallow。相机预览始终处于横向模式

package com.example.scanbarcode.scanbarcode; 

import android.Manifest; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

import java.io.File; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class TakePhotoActivity extends AppCompatActivity { 
    private Button takePictureButton; 
    private Button nextButton; 
    private ImageView imageView; 
    private Uri file; 
    private String FILE_NAME = "filename"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_take_photo); 

     takePictureButton = (Button) findViewById(R.id.button_image); 
     nextButton = (Button)findViewById(R.id.button_next); 
     imageView = (ImageView) findViewById(R.id.imageview); 

     nextButton.setEnabled(false); 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 
      takePictureButton.setEnabled(false); 
      ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0); 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     if (requestCode == 0) { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED 
        && grantResults[1] == PackageManager.PERMISSION_GRANTED) { 
       takePictureButton.setEnabled(true); 
      } 
     } 
    } 

    public void takePicture(View view) { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     file = Uri.fromFile(getOutputMediaFile()); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, file); 

     startActivityForResult(intent, 100); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 100) { 
      if (resultCode == RESULT_OK) { 
       imageView.setImageURI(file); 
       Intent intent1 = new Intent(); 
       intent1.putExtra(FILE_NAME, file.getPath()); 
       if ((file != null)&&(file.getPath().length() != 0)){ 
        nextButton.setEnabled(true); 
       } 
       setResult(RESULT_OK, intent1); 
      } 
     } 
    } 

    private static File getOutputMediaFile(){ 
     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "CameraDemo"); 

     if (!mediaStorageDir.exists()){ 
      if (!mediaStorageDir.mkdirs()){ 
       return null; 
      } 
     } 

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     return new File(mediaStorageDir.getPath() + File.separator + 
       "IMG_"+ timeStamp + ".jpg"); 
    } 

    public void nextOnClick(View view){ 
     this.finish(); 
    } 
} 

和布局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
    android:screenOrientation="portrait" 
    android:id="@+id/activity_take_photo" 
tools:context="com.example.scanbarcode.scanbarcode.TakePhotoActivity"> 


<ImageView 
    android:id="@+id/imageview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:id="@+id/button_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerInParent="true" 
    android:onClick="takePicture" 
    android:layout_alignBottom="@id/imageview" 
    android:text="Take a picture!"/> 

<Button 
    android:id="@+id/button_next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="Next" 
    android:layout_alignBottom="@id/imageview" 
    android:layout_toRightOf="@+id/button_image" 
    android:onClick="nextOnClick"/> 

</RelativeLayout> 
+0

https://stackoverflow.com/a/40159431/115145 https://stackoverflow.com/a/30596702/115145 – CommonsWare

+0

我没有在错误的角度保存图像的问题。预览不正确 – Eminem

+0

你指的是什么预览?如果您指的是相机应用中显示的预览,请联系相机应用的开发人员并要求他们修复其错误。 – CommonsWare

回答

0

没有“标准”相机应用程序。制造商经常自行发货。

无论如何,必须控制任何相机应用的内部行为相对于它的预览,至少通过标准ACTION_IMAGE_CAPTURE协议的手段。一些相机应用程序可能会公开自己的API,并且这样的API可能允许对预览进行一些控制。

因此,如果您在相机应用程序的预览中遇到可重现问题,请确定相机应用程序的开发人员,并尝试向他们提交错误报告。

+0

啊..好的。我现在遵循你所说的话。即时通讯使用Android模拟器。 Genymotion。好。所以只要你认为它是一个模拟器上的错误,并且应该在真实的设备上工作。 – Eminem

+0

@Eminem:呃,特别是对于相机,我只能在真实设备上进行测试。越接近实际硬件,我越不相信模拟器。在这种情况下,看看是否有已知的Genymotion问题。我有些惊讶,他们会有不同的方向来预览图片,因为他们没有任何感应器可以使用,但我没有使用Genymotion。 – CommonsWare