Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Bitmap 加載與像素操作

Android Bitmap 加載與像素操作

編輯:關於Android編程

Android Bitmap 加載與像素操作

一:加載與像素讀寫
在Android SDK中,圖像的像素讀寫可以通過getPixel與setPixel兩個Bitmap的API實現。Bitmap API讀取像素的代碼如下:

int pixel = bitmap.getPixel(col, row);// ARGB
int red = Color.red(pixel); // same as (pixel >> 16) &0xff
int green = Color.green(pixel); // same as (pixel >> 8) &0xff
int blue = Color.blue(pixel); // same as (pixel & 0xff)
int alpha = Color.alpha(pixel); // same as (pixel >>> 24)

得到像素pixel是32位的整數,四個字節分別對應透明通道、紅色、綠色、藍色通道。Bitmap API 寫入像素,代碼如下:

bm.setPixel(col, row, Color.argb(alpha, red, green, blue));

通過Color.argb重新組裝成一個int的像素值。
使用BitmapFactory.decodeFile或者decodeResource等方法實現加載圖像的Bitmap對象時,這些方法就會為要構建的Bitmap對象分配合適大小的內存,如果原始的圖像文件數據很大,就會導致DVM不能分配請求的內存大小,從而導致OOM(out of memory)問題。而通過配置BitmapFactory.Option預先讀取圖像高度與寬帶,圖像進行適當的下采樣,就可以避免OOM問題的發生。預先只獲取圖像高度與寬帶的代碼如下:

        // 獲取Bitmap圖像大小與類型屬性
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), 
                                        R.drawable.shar_03, options);
        int height = options.outHeight;
        int width = options.outWidth;
        String imageType = options.outMimeType;

基於下采樣加載超大Bitmap圖像的縮小版本:

        // 下采樣
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value 
            // that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        // 獲取采樣後的圖像顯示,避免OOM問題
        options.inJustDecodeBounds = false;
        srcImage = BitmapFactory.decodeResource(getResources(), R.drawable.shar_03, options);

二:像素操作
android彩色圖像灰度化的三個簡單方法
灰度化方法一:
灰度值GRAY = (max(red, green, blue) + min(red, green, blue))/2
灰度化方法二:
灰度值GRAY = (red + green + blue)/3
灰度化方法三:
灰度值GRAY = red*0.3 + green*0.59 + blue*0.11
代碼實現如下:

public Bitmap gray(Bitmap bitmap, int schema)
{
    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    for(int row=0; row> 16) &0xff
          int green = Color.green(pixel); // same as (pixel >> 8) &0xff
          int blue = Color.blue(pixel); // same as (pixel & 0xff)
          int alpha = Color.alpha(pixel); // same as (pixel >>> 24)
          int gray = 0;
          if(schema == 0)
          {
              gray = (Math.max(blue, Math.max(red, green)) + 
                          Math.min(blue, Math.min(red, green))) / 2;
          }
          else if(schema == 1)
          {
              gray = (red + green + blue) / 3;
          }
          else if(schema == 2)
          {
              gray = (int)(0.3 * red + 0.59 * green + 0.11 * blue);
          }
          bm.setPixel(col, row, Color.argb(alpha, gray, gray, gray));
       }
    }
    return bm;
}

Bitmap圖像鏡像映射與亮度調整的代碼實現如下:

public Bitmap brightness(Bitmap bitmap, double depth)
{
    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    for(int row=0; row> 16) &0xff
          int green = Color.green(pixel); // same as (pixel >> 8) &0xff
          int blue = Color.blue(pixel); // same as (pixel & 0xff)
          int alpha = Color.alpha(pixel); // same as (pixel >>> 24)
          double gray = (0.3 * red + 0.59 * green + 0.11 * blue);
          red += (depth * gray);
          if(red > 255) { red = 255; }

          green += (depth * gray);
          if(green > 255) { green = 255; }

          blue += (depth * gray);
          if(blue > 255) { blue = 255; }
          bm.setPixel(col, row, Color.argb(alpha, red, green, blue));
       }
    }
    return bm;
}

public Bitmap flip(Bitmap bitmap)
{
    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    for(int row=0; row> 16) &0xff
          int green = Color.green(pixel); // same as (pixel >> 8) &0xff
          int blue = Color.blue(pixel); // same as (pixel & 0xff)
          int alpha = Color.alpha(pixel); // same as (pixel >>> 24)
          int ncol = width - col - 1;
          bm.setPixel(ncol, row, Color.argb(alpha, red, green, blue));
       }
    }
    return bm;
}

運行截圖:
這裡寫圖片描述
布局XML文件內容如下:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">

MainActivity中的onCreate方法的代碼如下:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iView = (ImageView) this.findViewById(R.id.image_content);
Bitmap b = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
Canvas c = new Canvas(b);
c.drawText(Load Image from here..., 50, 200, paint);
iView.setImageBitmap(b);
Button saveBtn = (Button) this.findViewById(R.id.button_save);
saveBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        Toast toast = Toast.makeText(getApplicationContext(), Please load the image firstly..., Toast.LENGTH_SHORT);
        toast.show();
        loadImage();
        ImageView iView = (ImageView) findViewById(R.id.image_content);
        iView.setImageBitmap(srcImage);
        if(srcImage != null)
        {
            //saveFile(srcImage);
        }
    }

});
Button processBtn = (Button) this.findViewById(R.id.button_gray_3);
processBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        if(srcImage == null)
        {
            loadImage();
        }
        ImagePixelsProcessor processor = new ImagePixelsProcessor();
        Bitmap bm = processor.gray(srcImage, 2); // 有不同的灰度化策略
        final ImageView iView = (ImageView) findViewById(R.id.image_content);
        iView.setImageBitmap(bm);
    }

});

Button inverseBtn = (Button) this.findViewById(R.id.button_inverse);
inverseBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        if(srcImage == null)
        {
            loadImage();
        }
        ImagePixelsProcessor processor = new ImagePixelsProcessor();
        Bitmap bm = processor.brightness(srcImage, 0.3);
        final ImageView iView = (ImageView) findViewById(R.id.image_content);
        iView.setImageBitmap(bm);
    }
});

Button noRedBtn = (Button) this.findViewById(R.id.button_gray_1);
noRedBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        if(srcImage == null)
        {
            loadImage();
        }
        ImagePixelsProcessor processor = new ImagePixelsProcessor();
        Bitmap bm = processor.gray(srcImage, 0); // 有不同的灰度化策略
        final ImageView iView = (ImageView) findViewById(R.id.image_content);
        iView.setImageBitmap(bm);
    }
});

Button gray2Btn = (Button) this.findViewById(R.id.button_gray_2);
gray2Btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        if(srcImage == null)
        {
            loadImage();
        }
        ImagePixelsProcessor processor = new ImagePixelsProcessor();
        Bitmap bm = processor.gray(srcImage, 1); // 有不同的灰度化策略
        final ImageView iView = (ImageView) findViewById(R.id.image_content);
        iView.setImageBitmap(bm);
    }
});

Button flipBtn = (Button) this.findViewById(R.id.button_flip);
flipBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        if(srcImage == null)
        {
            loadImage();
        }
        ImagePixelsProcessor processor = new ImagePixelsProcessor();
        Bitmap bm = processor.flip(srcImage);
        final ImageView iView = (ImageView) findViewById(R.id.image_content);
        iView.setImageBitmap(bm);
    }
});

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved