[Android实例] 深入学习Bitmap-android的图片扫描

我这个实例是将一个位图用几秒钟扫描,然后得到它的反色图。做这个实例主要是想研究下Bitmap类和Color类,重点是getPixesetPix的这两个方法的用法。 

Java代码

1 publicclassSkanBitmapextendsActivity{

2 /**Calledwhentheactivityisfirstcreated.*/

3 privateBitmapbm;//要扫描的图片

4 privateBitmappm;//扫描后得到的图片

5 privateintwidth,height;//图片的宽和高

6 privateButtonbutton;

7 privateImageViewimageview;

8 @Override

9 publicvoidonCreate(BundlesavedInstanceState){

10 super.onCreate(savedInstanceState);

11 setContentView(R.layout.main);

12 button=(Button)findViewById(R.id.button);

13 imageview=(ImageView)findViewById(R.id.imageView);

14 bm=BitmapFactory.decodeResource(getResources(),R.drawable.sssd);//从资源文件中得到一个位图

15 width=bm.getWidth();//获取位图的宽

16 height=bm.getHeight();//获取位图的高

17 button.setOnClickListener(newOnClickListener(){

18 @Override

19 publicvoidonClick(Viewarg0){

20 //TODOAuto-generatedmethodstub

21 //新建一个宽是width,高是height,图片格式是RGB_565的空位图。

22 pm=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);

23 imageview.setImageBitmap(pm);

24 newthread().start();//开始扫描图片的线程

25 }

26 });

27 }

28 

29 //扫描图片的线程

30 classthreadextendsThread{

31 publicvoidrun(){

32 button.setClickable(false);//暂时将按钮的点击事件屏蔽掉,保证一次只开启一个线程

33 int[]colors=newint[width];//取一行图片的像素点

34 for(inti=0;i<height;i++){

35 //将位图bm的第i行的像素放入到数组colors中去

36 bm.getPixels(colors,0,width,0,i,width,1);

37 for(intj=0;j<colors.length;j++){

38 //将颜色数组中的RGB值取反,255减去当前颜色值就获得当前颜色的反色

39 colors[j]=Color.rgb(255-Color.red(colors[j]),

40 255-Color.green(colors[j]),255-Color.blue(colors[j]));

41 }

42 pm.setPixels(colors,0,width,0,i,width,1);//颜色取反后,将像素加入到pm的第i行中去

43 imageview.postInvalidate();//刷新图片

44 }

45 button.setClickable(true);//重新监听按钮的点击事件

46 

47 

48 }

49 }

50 

51 }


转载于:https://my.oschina.net/microoh/blog/147012

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值