package com.android.mypackage;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SkanBitmap extends Activity {
/** Called when the activity is first created. */
private Bitmap bm;//要扫描的图片
private Bitmap pm;//扫描后得到的图片
private int width,height;//图片的宽和高
private Button button;
private ImageView imageview;
private int threashold =125;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
imageview=(ImageView)findViewById(R.id.imageView);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inMutable = true;
bm=BitmapFactory.decodeResource(getResources(), R.drawable.sssd, opts);//从资源文件中得到一个位图
width=bm.getWidth();//获取位图的宽
height=bm.getHeight();//获取位图的高
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//disable button
button.setClickable(false);
//new thread for binaryzation
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//convert image into gray
Canvas c = new Canvas(bm);
Paint p = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
p.setColorFilter(filter);
c.drawBitmap(bm, 0, 0, p);
//binaryzation
int[] colors = new int[bm.getWidth()];
for(int i=0; i<bm.getHeight(); i++){
bm.getPixels(colors, 0, bm.getWidth(), 0, i, bm.getWidth(), 1);
for(int j=0; j<bm.getWidth(); j++){
int red = Color.red(colors[j]);
if(red > threashold){
red = 255;
}else{
red = 0;
}
colors[j]=Color.argb(255, red, red, red);
}
bm.setPixels(colors, 0, width, 0, i, width, 1);
}
mHandler.sendEmptyMessage(0);
}}).start();
// imageview.postInvalidate(); //刷新图片
// imageview.setImageBitmap(pm);
// new thread().start();//开始扫描图片的线程
}
});
}
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
//refresh image
imageview.setImageBitmap(bm);
//enable button
button.setClickable(false);
}};
// 扫描图片的线程
class thread extends Thread{
public void run() {
button.setClickable(false);//暂时将按钮的点击事件屏蔽掉,保证一次只开启一个线程
int[] colors=new int[width];//取一行图片的像素点
for(int i=0;i<height;i++){
//将位图bm的第i行的像素放入到数组 colors中去
bm.getPixels(colors, 0, width, 0, i, width, 1);
for(int j=0;j<colors.length;j++){
//将颜色数组中的RGB值取反,255减去当前颜色值就获得当前颜色的反色
colors[j]=Color.rgb((28*Color.red(colors[j]))>>8,
151*Color.green(colors[j])>>8,(77*Color.blue(colors[j]))>>8);
}
pm.setPixels(colors, 0, width, 0, i, width, 1);//颜色取反后,将像素加入到pm的第i行中去
imageview.postInvalidate();//刷新图片
}
button.setClickable(true);//重新监听按钮的点击事件
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Copy of skan.zip (38个子文件)
Copy of skan
.project 848B
project.properties 377B
proguard.cfg 1KB
src
com
android
mypackage
SkanBitmap.java 4KB
AndroidManifest.xml 736B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
sssd.jpg 373KB
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 2KB
drawable-xhdpi
values
strings.xml 174B
.settings
org.eclipse.jdt.core.prefs 598B
assets
gen
com
android
mypackage
BuildConfig.java 163B
R.java 850B
.classpath 475B
bin
resources.ap_ 383KB
classes
com
android
mypackage
R$layout.class 403B
SkanBitmap$1.class 1KB
BuildConfig.class 351B
R$drawable.class 444B
R$attr.class 349B
SkanBitmap$thread.class 2KB
SkanBitmap$2$1.class 2KB
R$id.class 426B
SkanBitmap.class 3KB
SkanBitmap$2.class 1KB
R.class 552B
R$string.class 436B
AndroidManifest.xml 736B
dexedLibs
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
crunch
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes.dex 7KB
Copy of skan.apk 389KB
skan.apk 74KB
共 38 条
- 1
小邪Mjustice
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页