Android: padding left a bitmap with white color

后端 未结 5 682
情话喂你
情话喂你 2020-12-16 14:30

How to set all white the 10 rows on the left side of a Bitmap? I\'v got a Bitmap that has to be padded on the left side. I thought i can create a new image iterate on the ol

相关标签:
5条回答
  • 2020-12-16 15:10

    Here's a kotlin extension function with RxJava to get it done. I haven't tested completely but based combined the previous answers to get something

    fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Single<Bitmap> {
        return Single.create<Bitmap> { emitter ->
            val output = Bitmap.createBitmap(
                (width + left + right).toInt(),
                (height + top + bottom).toInt(),
                Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(output)
    
            canvas.drawBitmap(this, left, top, null)
    
            emitter.onSuccess(output)
        }.subscribeOn(Schedulers.computation())
    }
    

    I think the coroutine version would simply be

    suspend fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Bitmap {
        val output = Bitmap.createBitmap(
            (width + left + right).toInt(),
            (height + top + bottom).toInt(),
            Bitmap.Config.ARGB_8888
        )
        val canvas = Canvas(output)
    
        canvas.drawBitmap(this, left, top, null)
    
        return output
    }
    
    0 讨论(0)
  • 2020-12-16 15:18

    You might want to look here:

    http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html

    methods you might want to use are: getHeight() then you know how many pixels to set and iterate over 10 columns

    and setRGB (int x, int y, int RGB) to set the pixel

    0 讨论(0)
  • 2020-12-16 15:20

    You can instead create a new Bitmap with the extra padding number of pixels. Set this as the canvas bitmap and Color the entire image with the required color and then copy your bitmap.

    public Bitmap pad(Bitmap Src, int padding_x, int padding_y) {
        Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
        Canvas can = new Canvas(outputimage);
        can.drawARGB(FF,FF,FF,FF); //This represents White color
        can.drawBitmap(Src, padding_x, padding_y, null);
        return outputimage;
    }
    
    0 讨论(0)
  • 2020-12-16 15:20
    public Bitmap addPaddingTopForBitmap(Bitmap bitmap, int paddingTop) {
        Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingTop, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawColor(Color.RED);
        canvas.drawBitmap(bitmap, 0, paddingTop, null);
        return outputBitmap;
    }
    
    public Bitmap addPaddingBottomForBitmap(Bitmap bitmap, int paddingBottom) {
        Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingBottom, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawColor(Color.RED);
        canvas.drawBitmap(bitmap, 0, 0, null);
        return outputBitmap;
    }
    
    
    public Bitmap addPaddingRightForBitmap(Bitmap bitmap, int paddingRight) {
        Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingRight, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawColor(Color.RED);
        canvas.drawBitmap(bitmap, 0, 0, null);
        return outputBitmap;
    }
    
    public Bitmap addPaddingLeftForBitmap(Bitmap bitmap, int paddingLeft) {
        Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingLeft, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawColor(Color.RED);
        canvas.drawBitmap(bitmap, paddingLeft, 0, null);
        return outputBitmap;
    }
    
    0 讨论(0)
  • 2020-12-16 15:25

    I just did this to give padding from all side. Hope it will help someone. Combination of https://stackoverflow.com/a/44060669/6480433 & https://stackoverflow.com/a/6957333/6480433 these answer.

    Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
            Canvas can = new Canvas(outputimage);
            can.drawBitmap(Src, padding_x, padding_y, null);
    
            Bitmap output = Bitmap.createBitmap(outputimage.getWidth()+padding_x, outputimage.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            canvas.drawBitmap(outputimage, 0, 0, null);    
      return output;
    
    0 讨论(0)
提交回复
热议问题