安卓 图片处理
16lz
2021-01-26
MainActivitypackage com.example.and6;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.ColorMatrix;import android.graphics.ColorMatrixColorFilter;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.support.v4.view.ScrollingView;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.ScrollView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity implements OnSeekBarChangeListener{int MID_VALUE=50;ImageView iv;Bitmap bm;float hue = 0,sta = 0.5f,lum = 0.5f;SeekBar seekBar1;SeekBar seekBar2;SeekBar seekBar3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main3);bm=BitmapFactory.decodeResource(getResources(), R.drawable.pic);// seekBar1=(SeekBar) findViewById(R.id.seekBar1); seekBar2=(SeekBar) findViewById(R.id.seekBar2); seekBar3=(SeekBar) findViewById(R.id.seekBar3);iv=(ImageView) findViewById(R.id.iv);iv.setImageBitmap(bm);seekBar1.setOnSeekBarChangeListener(this);seekBar2.setOnSeekBarChangeListener(this);seekBar3.setOnSeekBarChangeListener(this);findViewById(R.id.next).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubstartActivity(new Intent(getApplicationContext(), MainActivity2.class));}});}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {// TODO Auto-generated method stubswitch (seekBar.getId()) {case R.id.seekBar1: hue=(progress -MID_VALUE)*1.0F/MID_VALUE*100;break;case R.id.seekBar2: sta=progress*1.0F/MID_VALUE;break;case R.id.seekBar3: lum=progress*1.0F/MID_VALUE;break;default:break;} System.out.println(seekBar1.getProgress()+" "+seekBar2.getProgress()+" "+seekBar3.getProgress());iv.setImageBitmap(createBitmap( bm ,hue,sta,lum ));} private Bitmap createBitmap(Bitmap bm, float hue, float sta, float lum) {// TODO Auto-generated method stubBitmap bmp=Bitmap.createBitmap( bm.getWidth(), bm.getHeight(), Config.ARGB_8888);Canvas canvas=new Canvas(bmp);Paint paint=new Paint();ColorMatrix hueMat=new ColorMatrix();hueMat.setRotate(0, hue);hueMat.setRotate(1, hue);hueMat.setRotate(2, hue);ColorMatrix staMat=new ColorMatrix();staMat.setSaturation(sta);ColorMatrix lumMat=new ColorMatrix();lumMat.setScale(lum, lum, lum, 1);ColorMatrix mat=new ColorMatrix();mat.postConcat(hueMat);mat.postConcat(staMat);mat.postConcat(lumMat);paint.setColorFilter(new ColorMatrixColorFilter(mat));canvas.drawBitmap(bm, 0, 0,paint);return bmp;}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) { }@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {} }
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="vertical" android:padding="10dp" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="300dp" android:src="@drawable/pic" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="色调hue" android:textColor="#000" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:padding="10dp" android:progress="50" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="饱和度sat" android:textColor="#000" /> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:padding="10dp" android:progress="50" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="亮度lum" android:textColor="#000" /> <SeekBar android:id="@+id/seekBar3" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:padding="10dp" android:progress="50" /> <Button android:id="@+id/next" android:text="open" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout></ScrollView>
MainActivity2package com.example.and6;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.ColorMatrix;import android.graphics.ColorMatrixColorFilter;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.support.v4.view.ScrollingView;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.EditText;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.ScrollView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity2 extends Activity { ImageView iv;Bitmap bitmap; EditText edts[]=new EditText[20]; float[] colors=new float[20]; float[]dark=new float[]{ 0.33f,0.59f , 0.11f , 0, 0, 0.33f, 0.59f, 0.11f , 0, 0, 0.33f, 0.59f, 0.11f , 0, 0, 0, 0, 0, 1,0 }; float[]reverse=new float[]{ -1,0,0,1,1, 0,-1,0,1,1, 0,0,-1,1,1, 0,0,0,1,0 }; float[]old=new float[]{ 0.393f, 0.769f, 0.189f, 0 , 0, 0.349f, 0.686f, 0.168f,0 , 0, 0.272f, 0.534f, 0.131f ,0 ,0, 0, 0 , 0 , 1, 0 }; float[]quse=new float[]{ 1.5f , 1.5f , 1.5f , 0 , -1, 1.5f , 1.5f , 1.5f , 0 , -1, 1.5f , 1.5f , 1.5f , 0 , -1, 0 , 0, 0, 1, 0 }; float[]baohe=new float[]{ 1.438f, -0.122f, -0.016f,0 ,-0.03f, -0.062f,1.378f, -0.016f, 0, 0.05f, -0.062f,-0.122f,1.483f,0, -0.02f, 0,0,0,1,0 }; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main4);edts[0]=(EditText) findViewById(R.id.edt1);edts[1]=(EditText) findViewById(R.id.edt2);edts[2]=(EditText) findViewById(R.id.edt3);edts[3]=(EditText) findViewById(R.id.edt4);edts[4]=(EditText) findViewById(R.id.edt5);edts[5]=(EditText) findViewById(R.id.edt6);edts[6]=(EditText) findViewById(R.id.edt7);edts[7]=(EditText) findViewById(R.id.edt8);edts[8]=(EditText) findViewById(R.id.edt9);edts[9]=(EditText) findViewById(R.id.edt10);edts[10]=(EditText) findViewById(R.id.edt11);edts[11]=(EditText) findViewById(R.id.edt12);edts[12]=(EditText) findViewById(R.id.edt13);edts[13]=(EditText) findViewById(R.id.edt14);edts[14]=(EditText) findViewById(R.id.edt15);edts[15]=(EditText) findViewById(R.id.edt16);edts[16]=(EditText) findViewById(R.id.edt17);edts[17]=(EditText) findViewById(R.id.edt18);edts[18]=(EditText) findViewById(R.id.edt19);edts[19]=(EditText) findViewById(R.id.edt20);iv=(ImageView) findViewById(R.id.iv);bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.pic);iv.setImageBitmap(bitmap);findViewById(R.id.reset).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubinitMat();getMat();setImag();}}); findViewById(R.id.run).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {getMat();setImag();}});findViewById(R.id.dark).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfor (int i = 0; i < edts.length; i++) {edts[i].setText(""+dark[i]);}getMat();setImag();}});findViewById(R.id.reverse).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfor (int i = 0; i < edts.length; i++) {edts[i].setText(""+reverse[i]);}getMat();setImag();}});findViewById(R.id.old).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfor (int i = 0; i < edts.length; i++) {edts[i].setText(""+old[i]);}getMat();setImag();}});findViewById(R.id.quse).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfor (int i = 0; i < edts.length; i++) {edts[i].setText(""+quse[i]);}getMat();setImag();}});findViewById(R.id.baohe).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfor (int i = 0; i < edts.length; i++) {edts[i].setText(""+baohe[i]);}getMat();setImag();}});} private void getMat() {for (int i = 0; i < edts.length; i++) {colors[i]=Float.valueOf( edts[i].getText().toString() );}} private void setImag() {Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);ColorMatrix colorMat = new ColorMatrix();colorMat.set(colors);Canvas canvas = new Canvas(bmp);Paint paint = new Paint();paint.setColorFilter(new ColorMatrixColorFilter(colorMat));canvas.drawBitmap(bitmap, 0, 0, paint);iv.setImageBitmap(bmp);} private void initMat() {for (int i = 0; i < edts.length; i++) {if (i%6==0) {edts[i].setText("1");}elseedts[i].setText("0"); }} }
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="vertical" android:padding="10dp" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="300dp" android:src="@drawable/pic" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edt1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="1" android:textColor="#000" /> <EditText android:id="@+id/edt2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edt6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="1" android:textColor="#000" /> <EditText android:id="@+id/edt8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt10" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edt11" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt12" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt13" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="1" android:textColor="#000" /> <EditText android:id="@+id/edt14" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt15" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/edt16" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt17" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt18" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> <EditText android:id="@+id/edt19" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="1" android:textColor="#000" /> <EditText android:id="@+id/edt20" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="3dp" android:layout_weight="1.0" android:background="#eee" android:padding="8dp" android:text="0" android:textColor="#000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/reset" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="重置" /> <Button android:id="@+id/run" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="执行" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/dark" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="灰度" /> <Button android:id="@+id/reverse" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="翻转" /> <Button android:id="@+id/old" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="怀旧" /> <Button android:id="@+id/quse" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="去色" /> <Button android:id="@+id/baohe" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="饱和" /> </LinearLayout> </LinearLayout></ScrollView>
更多相关文章
- 仿照DatePickerFragment实现的TimePickerFragment
- 【Andriod】Andriod-Opencv 实现一些简单的滤镜功能
- javaapiOpenCV4Android--使用java api做人脸识别
- android为图片去色,返回灰度图片
- Android提高十六篇之使用NDK把彩图转换灰度图
- Android(安卓)NDK开发及OpenCV初步学习笔记
- Android(安卓)将View 转化为bitmap 图片
- Android(安卓)-- 重置Bitmap大小&&Bitmap转角度
- Hello Android(安卓)- Bitmap转换为黑白的灰度图和加圆角效果