2015年3月15日 星期日

Android-美圖秀秀 APP_色相_飽和度_亮度( 一 )

以下圖片修改是我們在PS最常用的功能
這次我們藉由小小的範例來呈現商城或內建程式是怎執行的
這次資料來源來自於慕課網的教學範例
希望想進一步了解的大大們,可以善用。

藉由SeekBar調整圖片的色相、飽和度、亮度

MainActivity.java
package com.example.image;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{
    
    private ImageView mImageView;
    private SeekBar mSeekbarhue,mSeekbarSaturation, mSeekbarLum;
    private static int MAX_VALUE = 255;
    private static int MID_VALUE = 127;
    private float mHue,mStauration, mLum;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flower);
        
        mImageView = (ImageView) findViewById(R.id.imageView1);
        mSeekbarhue = (SeekBar) findViewById(R.id.seekBar1);
        mSeekbarSaturation = (SeekBar) findViewById(R.id.seekBar2);
        mSeekbarLum = (SeekBar) findViewById(R.id.seekBar3);
        
        mSeekbarhue.setOnSeekBarChangeListener(this);
        mSeekbarSaturation.setOnSeekBarChangeListener(this);
        mSeekbarLum.setOnSeekBarChangeListener(this);
        
        mSeekbarhue.setMax(MAX_VALUE);
        mSeekbarSaturation.setMax(MAX_VALUE);
        mSeekbarLum.setMax(MAX_VALUE);
        
        mSeekbarhue.setProgress(MID_VALUE);
        mSeekbarSaturation.setProgress(MID_VALUE);
        mSeekbarLum.setProgress(MID_VALUE);
        
        mImageView.setImageBitmap(bitmap);
        
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        switch(seekBar.getId()){
        case R.id.seekBar1:
            mHue = (progress-mHue)*1.0F / MID_VALUE*180;
            break;
        case R.id.seekBar2:
            mStauration = progress*1.0F / MID_VALUE;
            break;
        case R.id.seekBar3:
            mLum = progress*1.0F / MID_VALUE;
            break;
        }
        mImageView.setImageBitmap(ImageHelper.handleImageEffect(bitmap, mHue, mStauration, mLum));
        
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        
    }

}


ImageHelper.java
public class ImageHelper {

    public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
        
        // 由於不能直接修改,所以要複製一份
        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // 色調
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue);
        hueMatrix.setRotate(1, hue);
        hueMatrix.setRotate(2, hue);

        // 飽和度
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        // 亮度
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);

        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bm, 0, 0, paint);

        return bmp;
    }

}


原圖 :
























亂調 :























歡迎轉載,請註明出處。



沒有留言:

張貼留言