【问题标题】:How can I crop and rotate image programmatically in Android?如何在 Android 中以编程方式裁剪和旋转图像?
【发布时间】:2021-11-16 13:08:39
【问题描述】:

我想将图像旋转 90 度,还想裁剪从手机图库中取出的图像。如何在 Android 中以编程方式执行此操作?

【问题讨论】:

    标签: android image


    【解决方案1】:

    要执行图像的旋转,您可以使用以下代码:

    Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
    Matrix mat = new Matrix();
    mat.postRotate(90);
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
                                 bMap.getWidth(), bMap.getHeight(), mat, true);
    BitmapDrawable bmd = new BitmapDrawable(bMapRotate);
    image.setImageBitmap(bMapRotate);
    image.setImageDrawable(bmd);
    

    对于从图库中截取的图像,请使用以下 sn-p 代码:

        Intent viewMediaIntent = new Intent();
        viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File("/image/*");    
        viewMediaIntent.setDataAndType(Uri.fromFile(file), "image/*");   
        viewMediaIntent.putExtra("crop","true");
        viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivityForResult(viewMediaIntent,1);
    

    【讨论】:

    • ERROR/(5824): can't open '/image/*' 为什么?你有什么想法吗?
    • 我无法使用上述代码从图库中打开图片。它显示多个选项来裁剪
    【解决方案2】:

    尝试使用以下代码从图库中裁剪所选图像。

     private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg";  
    
     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     photoPickerIntent.setType("image/*");
     photoPickerIntent.putExtra("crop", "true");
     photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
     photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
     startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE);
    
    
    private Uri getTempUri() {
    return Uri.fromFile(getTempFile());
    }
    
    private File getTempFile() {
    if (isSDCARDMounted()) {
    
    File f = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
    try {
    f.createNewFile();
    } catch (IOException e) {
    
    }
    return f;
    } else {
    return null;
    }
    }
    
    private boolean isSDCARDMounted(){
    String status = Environment.getExternalStorageState();
    if (status.equals(Environment.MEDIA_MOUNTED))
    return true;
    return false;
    }
    
    
    
    
     protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
    switch (requestCode) {
    case REQ_CODE_PICK_IMAGE:
        if (resultCode == RESULT_OK) {  
          if (imageReturnedIntent!=null){
    
    
    
               File tempFile = getTempFile();
    
              String filePath= Environment.getExternalStorageDirectory()
            + "/temporary_holder.jpg";
              System.out.println("path "+filePath);
    
    
    Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
    _image = (ImageView) findViewById(R.id.image);
    _image.setImageBitmap(selectedImage );
    
     }
     }
     }
    

    也看看这个Tutorial1,tutorial2

    【讨论】:

    • 这段代码先打开图库,我已经通过tutorial2实现了,我想打开特定的图像进行裁剪,而不是从图库中选择,图片是默认存在的...你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多