Android-將RGB彩色圖轉換為灰階圖,

來源:互聯網
上載者:User

Android-將RGB彩色圖轉換為灰階圖,

package com.example.yanlei.wifi;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    /* (non-Javadoc)    * @see android.app.Activity#onCreate(android.os.Bundle)    */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //通過Id來擷取介面中組件的引用        Button rgb2greyBtn  = (Button) findViewById(R.id.rgb2greybtn);        ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);        final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);        //通過位元影像工廠,建立一個位元影像        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.c);        imageView1.setImageBitmap(bitmap);        //為“轉換為灰階圖”按鈕添加監聽事件        rgb2greyBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                //將轉換過後的灰階圖顯示出來                imageView2.setImageBitmap(convertGreyImg(bitmap));            }        });    }    /**     * 將彩色圖轉換為灰階圖     * @param img 位元影像     * @return  返迴轉換好的位元影像     */    public Bitmap convertGreyImg(Bitmap img) {        int width = img.getWidth();         //擷取位元影像的寬        int height = img.getHeight();       //擷取位元影像的高        int []pixels = new int[width * height]; //通過位元影像的大小建立像素點數組        img.getPixels(pixels, 0, width, 0, 0, width, height);        int alpha = 0xFF << 24;        for(int i = 0; i < height; i++)  {            for(int j = 0; j < width; j++) {                int grey = pixels[width * i + j];                int red = ((grey  & 0x00FF0000 ) >> 16);                int green = ((grey & 0x0000FF00) >> 8);                int blue = (grey & 0x000000FF);                grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);                grey = alpha | (grey << 16) | (grey << 8) | grey;                pixels[width * i + j] = grey;            }        }        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);        result.setPixels(pixels, 0, width, 0, 0, width, height);        return result;    }}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        />    <Button        android:id="@+id/rgb2greybtn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/rgb2greybtn"        android:layout_gravity="center_horizontal"/>    <ImageView        android:id="@+id/imageView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        />"</LinearLayout>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.