一、main/assets路径下有raw图像文件,是一些图片的数据,这些是指纹图像。
二、现在我们需要读取出raw数据然后显示出来。
三、核心源码
1、
2、srcmainjavacomwfhcanvasdemoDataUtil.java
package com.wfh.canvasdemo; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; public class DataUtil { private byte[] image; public static String strarry1[] = { "01_00_00.raw", "01_00_01.raw", "01_00_02.raw", "01_00_03.raw", "01_00_04.raw", "01_00_05.raw", "01_00_06.raw", "01_00_07.raw", "01_00_08.raw", "01_00_09.raw", "01_00_10.raw", "01_00_11.raw", "01_00_12.raw", "01_00_13.raw", "01_00_14.raw", "01_00_15.raw", "01_00_16.raw", "01_00_17.raw", "01_00_18.raw", "01_00_19.raw", "01_00_20.raw", "01_00_21.raw", "01_00_22.raw", "01_00_23.raw", "01_00_24.raw", "01_00_25.raw", "01_00_26.raw", "01_00_27.raw", "01_00_28.raw", "01_00_29.raw", "01_00_30.raw", "01_00_31.raw", "01_00_32.raw", "01_00_33.raw" }; public static String strarry2[] = { "02_00_00.raw", "02_00_01.raw", "02_00_02.raw", "02_00_03.raw" }; private byte[] readStream(InputStream inStream) throws Exception { //byte[] buffer = new byte[15600]; byte[] buffer = new byte[35600]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } public byte[] setRawData(Context context, String fileName) throws Exception { try { image = null; InputStream inputStream = context.getResources().getAssets() .open(fileName); image = readStream(inputStream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return image; } }
2、srcmainjavacomwfhcanvasdemoMainActivity.java
package com.wfh.canvasdemo; import java.io.File; import java.io.FileOutputStream; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import static java.lang.Long.toHexString; public class MainActivity extends Activity implements View.OnTouchListener{ private Button btn_save, btn_resume; private ImageView iv_canvas; private Bitmap baseBitmap; private Canvas canvas; private Paint paint; // 定义手指开始触摸的坐标 float startX; float startY; //int[] data= new int[25600]; byte[] image = null; private DataUtil mDataUtil; String[] flies = null; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 用户按下动作 case MotionEvent.ACTION_DOWN: int x,y; // 第一次绘图初始化内存图片,指定背景为白色 if (baseBitmap == null) { baseBitmap = Bitmap.createBitmap(iv_canvas.getWidth(), iv_canvas.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.YELLOW); } // 记录开始触摸的点的坐标 startX = event.getX(); startY = event.getY(); String str="#000000"; x=0; int m; try { image = mDataUtil.setRawData(MainActivity.this, "1" + "/" + "01_00_00.raw"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (image == null) { Toast.makeText(MainActivity.this, "Failed to read image", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "success to read image", Toast.LENGTH_SHORT).show(); for(y=0;y<160;y++) { for (x = 0; x < 160; x++) { m=(int)image[159-x+y*160]&0xff; if(m<0x10) str = "#" +"0"+toHexString(m) +"0"+ toHexString(m)+"0" + toHexString(m); else str = "#" + toHexString(m) + toHexString(m) + toHexString(m); paint.setColor(Color.parseColor(str)); canvas.drawPoint(startX + 3*x, startY+3*y, paint); } } } iv_canvas.setImageBitmap(baseBitmap); break; // 用户手指在屏幕上移动的动作 case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; default: break; } return true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化一个画笔,笔触宽度为5,颜色为红色 paint = new Paint(); // paint.setStrokeWidth(20); paint.setStrokeWidth(3); paint.setColor(Color.RED); iv_canvas = (ImageView) findViewById(R.id.iv_canvas); btn_save = (Button) findViewById(R.id.btn_save); btn_resume = (Button) findViewById(R.id.btn_resume); btn_save.setonClickListener(click); btn_resume.setonClickListener(click); // iv_canvas.setonTouchListener(touch); iv_canvas.setonTouchListener(this); mDataUtil = new DataUtil(); } private View.onClickListener click = new onClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_save: saveBitmap(); break; case R.id.btn_resume: resumeCanvas(); break; default: break; } } }; protected void saveBitmap() { try { // 保存图片到SD卡上 File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".png"); FileOutputStream stream = new FileOutputStream(file); baseBitmap.compress(CompressFormat.PNG, 100, stream); Toast.makeText(MainActivity.this, "保存图片成功", Toast.LENGTH_SHORT).show(); // Android设备Gallery应用只会在启动的时候扫描系统文件夹 // 这里模拟一个媒体装载的广播,用于使保存的图片可以在Gallery中查看 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment .getExternalStorageDirectory())); sendBroadcast(intent); } catch (Exception e) { Toast.makeText(MainActivity.this, "保存图片失败", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } protected void resumeCanvas() { // 手动清除画板的绘图,重新创建一个画板 if (baseBitmap != null) { baseBitmap = Bitmap.createBitmap(iv_canvas.getWidth(), iv_canvas.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.YELLOW); iv_canvas.setImageBitmap(baseBitmap); Toast.makeText(MainActivity.this, "清除画板成功,可以重新开始绘图", Toast.LENGTH_LONG).show(); } } }
四、运行测试效果图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)