位图旋转并保存图像

时间:2016-07-01 10:15:27

标签: android

我想旋转并保存旋转的图像并将其移动到我的Android设备中的其他位置。

  1. 我可以旋转图像并将其设置为图像视图。
  2. 我可以将UN-ROTATED图像复制到我选择的目的地。
  3. 我唯一无法做的是获取保存的旋转图像FILE(rotate.jpg)

    我的下面的代码要旋转:(这不会将旋转的文件保存到存储?)

                  Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 
    
                  Matrix matrix = new Matrix();
                  matrix.postRotate(getImageOrientation(filePathLocal));
                  rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);          
    
                  //Set Image
                  ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile);
                  ibProfile.setImageBitmap(rotatedBitmap);
    

    现在上面只是暂时旋转,直到活动结束,现在我想从上面的代码中保存这个旋转的图像,并在将其上传到服务器之前移动它,我已经知道如何复制/移动文件并上传所以没有需要发布这些代码 - 我需要的只是保存旋转图像的代码,所以我有类似/sdcard/saved_rotated_image.jpg

2 个答案:

答案 0 :(得分:2)

   Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 

          Matrix matrix = new Matrix();
          matrix.postRotate(getImageOrientation(filePathLocal));
          rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);          

          //Set Image
          ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile);
          ibProfile.setImageBitmap(rotatedBitmap);
  

在createbitmap的最后一个参数

中输入false

参考我的代码:

@Override
    protected Uri doInBackground(String... params) {
        String filepath = params[0];
        String filename = params[1];
        String filetype = params[2];

        Bitmap bitmap = takeScreenShot(root);
        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postRotate(Datas.rotationvalue);
        Log.e("rotationvalue", Datas.rotationvalue+"...");
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, false);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        try {
            File f = new File(filepath); 
            if (!f.exists()) {
                f.mkdir();
            }
            String folderpath = f.toString();
            File file = new File(folderpath, filename + "." + filetype);

            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            fo.close();
            Uri uri = Uri.fromFile(file);
            Log.e("Edited img uri", uri.toString());
            return uri;
        } catch (Exception e) {
            Log.e("Exception...occured", e.toString());
            e.printStackTrace();
            return null;
        }
    }

此代码工作正常。在我自己的一边,试着在你身边。

  

根据doc:last参数是filter boolean:如果是源,则为true   应该过滤。仅适用于矩阵包含的不仅仅是   翻译。

链接了解更多信息:Last argument more info

答案 1 :(得分:1)

保存图像功能如下:



	private void saveBitmap(Bitmap bitmap, String fileName) {
		File file = new File(Environment.getExternalStorageDirectory(), fileName);
		FileOutputStream fileOS = null;
		try {
			fileOS = new FileOutputStream(file);
			// quality: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality.
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS);
			fileOS.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOS != null) {
				try {
					fileOS.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}




使用样本:



   Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 

   Matrix matrix = new Matrix();
   matrix.postRotate(getImageOrientation(filePathLocal));
   rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);          

   saveBitmap(rotatedBitmap, "saved_rotated_image.jpg");