【问题标题】:Flip a Bitmap image horizontally or vertically水平或垂直翻转位图图像
【发布时间】:2016-07-29 09:14:51
【问题描述】:

通过使用此代码,我们可以旋转图像:

public static Bitmap RotateBitmap(Bitmap source, float angle) {
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

但是我们怎样才能水平或垂直翻转图像呢?

【问题讨论】:

    标签: java android image matrix android-bitmap


    【解决方案1】:

    鉴于cx,cy 是图像的中心:

    翻转x:

    matrix.postScale(-1, 1, cx, cy);
    

    翻转y:

    matrix.postScale(1, -1, cx, cy);
    

    总共:

    public static Bitmap createFlippedBitmap(Bitmap source, boolean xFlip, boolean yFlip) {
        Matrix matrix = new Matrix();
        matrix.postScale(xFlip ? -1 : 1, yFlip ? -1 : 1, source.getWidth() / 2f, source.getHeight() / 2f);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }
    

    【讨论】:

    • 如何找到图像的中心?
    • @MayurRokade source.getWidth()/2f source.getHeight()/2f
    【解决方案2】:

    Kotlin 的简短扩展

    private fun Bitmap.flip(x: Float, y: Float, cx: Float, cy: Float): Bitmap {
        val matrix = Matrix().apply { postScale(x, y, cx, cy) }
        return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
    }
    

    及用法:

    对于水平翻转:-

    val cx = bitmap.width / 2f
    val cy = bitmap.height / 2f
    val flippedBitmap = bitmap.flip(-1f, 1f, cx, cy)
    ivMainImage.setImageBitmap(flippedBitmap)
    

    垂直翻转:-

    val cx = bitmap.width / 2f
    val cy = bitmap.height / 2f
    val flippedBitmap = bitmap.flip(1f, -1f, cx, cy)
    ivMainImage.setImageBitmap(flippedBitmap)
    

    【讨论】:

    • 我会将cx 和cy 计算放在flip 方法中。 Matrix().apply { postScale(x, y, width / 2f, height / 2f) } 此外,您的 flip 方法与 scale 方法扩展没有什么不同。
    【解决方案3】:

    对于 kotlin,

    fun Bitmap.flip(): Bitmap {
        val matrix = Matrix().apply { postScale(-1f, 1f, width/2f, width/2f) }
        return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
    }
    

    【讨论】:

      【解决方案4】:

      只需使用以下代码

          private fun setVerticalFlip() {
          if (binding.imgReal.scaleX == 1.0f) {
              binding.imgReal.scaleX = -1.0f
          } else {
              binding.imgReal.scaleX = 1.0f
          }
      }
      
      private fun setHorizontalFlip() {
          if (binding.imgReal.scaleY == 1.0f) {
              binding.imgReal.scaleY = -1.0f
          } else {
              binding.imgReal.scaleY = 1.0f
          }
      }
      

      【讨论】:

        【解决方案5】:

        位图 bms 的水平和垂直翻转(来源)。

        Matrix matrix = new Matrix();
        // for horizontal flip
        matrix.setScale(-1, 1);
        matrix.postTranslate( bms.getWidth(),0);
        // for vertical flip
        matrix.setScale( 1,-1);
        matrix.postTranslate( 0, bms.getHeight());
        Bitmap bm = Bitmap.createBitmap( bms, 0, 0, bms.getWidth(), bms.getHeight(), matrix, true);
        

        【讨论】:

          【解决方案6】:

          一切都与您使用的矩阵有关。要绕 x 轴翻转它,请使用 [[-1,0],[0, 1]]。对于 y 轴,使用 [[1,0],[0,-1]]。这里重要的是行列式的绝对值是 1,所以它不会缩放。而 - 基本上反转了给定轴周围的位置。

          【讨论】:

          • 问题是,100 的 x 变为 -100 的 x,这需要发生在图像的中心附近,而不是原点。
          • 然后先翻译画布。
          • 如果你的意思是对矩阵应用翻译,那么你也必须在之后翻译。即你可以translate(-cx/2,-cy/2), scale, translate(cx/2,cy/2).
          猜你喜欢
          • 2017-03-27
          • 2015-12-28
          • 2016-07-17
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-07
          相关资源
          最近更新 更多