我正在使用Android Studio。当我执行代码拍照时,预览始终以横向模式显示,但图像以纵向保存 如何让预览以纵向模式显示?
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 :(得分:0)
没有&#34;标准&#34;相机应用程序制造商通常会自己发货。
无论如何,至少通过标准ACTION_IMAGE_CAPTURE
协议,您无法控制任何相机应用程序的预览内部行为。某些相机应用可能会公开自己的API,而这样的API可能会允许对预览进行一些控制。
因此,如果您有相机应用预览的可重现问题,请确定相机应用的开发者,并尝试向他们提交错误报告。