【问题标题】:After taking picture from camera image is not fitting to full screen in android从相机图像拍照后不适合在 android 中全屏显示
【发布时间】:2016-09-07 15:30:19
【问题描述】:

下面是我的代码,如果在设置为二次采样图像视图之后从相机拍摄照片,则将图像尺寸设置为小,而不是全屏。

public class MainActivity extends Activity {

Bitmap thumbnail;
private int REQUEST_CAMERA = 0;
private Button btnSelect;
private String userChoosenTask;
ArrayList<Uri> mArrayUri;
SubsamplingScaleImageView ivImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
    ivImage = (SubsamplingScaleImageView) findViewById(R.id.ivImage);

    btnSelect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

    mArrayUri = new ArrayList<Uri>();
  
}

private void selectImage() {
    final CharSequence[] items = {"Take Photo",
            "Cancel"};

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (items[item].equals("Take Photo")) {
                    cameraIntent();

            }else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
         if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

private void onCaptureImageResult(Intent data) {
    thumbnail = (Bitmap) data.getExtras().get("data");
    mArrayUri.add(getImageUri(MainActivity.this, thumbnail));
    ivImage.setImage(ImageSource.bitmap(thumbnail).dimensions(4128, 3096));
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
  }
}

Xml 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_weight="0.1"
    android:gravity="center">

    <Button
        android:id="@+id/btnSelectPhoto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Photo" />
</LinearLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_weight="2"
    android:gravity="center"
    android:orientation="vertical">

    <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
        android:id="@+id/ivImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

   </LinearLayout>

</LinearLayout>

Gradle 添加这些行

 compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'

谁能帮我提前谢谢。

【问题讨论】:

    标签: android imageview android-camera


    【解决方案1】:
     private void onCaptureImageResult(Intent data) {
    
           //mArrayUri.add(getImageUri(MainActivity.this, thumbnail));
            String ImagePath = getRealPathFromURI(context, data.getData());
            ivImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));
        }
    
        public String getRealPathFromURI(Context context, Uri contentUri) {
            Cursor cursor = null;
            try {
                String[] proj = {MediaStore.Images.Media.DATA};
                cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
    

    【讨论】:

    • 等我试试
    • 我们不能这样设置它是 SubsamplingScaleImageView ivImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));
    【解决方案2】:

    基本上thumbnail = (Bitmap) data.getExtras().get("data"); 将只返回拇指大小的图像,这对于仅设置小拇指大小的图像很有用。如果您尝试在全屏中设置拇指大小的图像,它可能会失真。因此,我建议将您的代码更改为全尺寸图像。 要在此处获取全尺寸图像,请参阅 google 提供的相机意图演示应用程序的链接。 https://developer.android.com/training/camera/photobasics.html

    【讨论】:

      【解决方案3】:

      设置好图片后,调用ivImage.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP),你可以修改你的代码如下:

      private void onCaptureImageResult(Intent data) {
          thumbnail = (Bitmap) data.getExtras().get("data");
          mArrayUri.add(getImageUri(MainActivity.this, thumbnail));
          ivImage.setImage(ImageSource.bitmap(thumbnail).dimensions(4128, 3096));
          ivImage.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
      }
      

      从这个imageView 库的configuration doc 获得这个。

      【讨论】:

      • 您可以参考并尝试文档中提供的其他配置设置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2016-11-17
      • 2021-08-13
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多