Android 画布逐点绘制 公共类MainActivity扩展了ActionBarActivity实现OnClickListener{ 按钮btn; 图像视图; 位图bm; 帆布; 油漆; @凌驾 创建时受保护的void(Bundle savedInstanceStat

Android 画布逐点绘制 公共类MainActivity扩展了ActionBarActivity实现OnClickListener{ 按钮btn; 图像视图; 位图bm; 帆布; 油漆; @凌驾 创建时受保护的void(Bundle savedInstanceStat,android,Android,画布逐点绘制 公共类MainActivity扩展了ActionBarActivity实现OnClickListener{ 按钮btn; 图像视图; 位图bm; 帆布; 油漆; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(按钮)super.findViewById(R.id.button1);

画布逐点绘制
公共类MainActivity扩展了ActionBarActivity实现OnClickListener{
按钮btn;
图像视图;
位图bm;
帆布;
油漆;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(按钮)super.findViewById(R.id.button1);
btn.setOnClickListener(此);
视图=(ImageView)super.findViewById(R.id.imageView1);
Bitmap.Config conf=Bitmap.Config.ARGB_8888;
bm=Bitmap.createBitmap(200200conf);
画布=新画布(bm);
油漆=新油漆(油漆.防油漆别名标志);
油漆。设置颜色(颜色。黑色);
帆布。画圈(50,50,10,油漆);
view.setImageBitmap(bm);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
尝试
{
//从定制Kakadu中的错误和警告服务开始
Kdu_sysout_消息sysout=
新Kdu_sysout_消息(false);//非抛出消息打印机
Kdu_sysout_消息syserr=
新建Kdu_sysout_消息(true);//异常引发消息打印机
Kdu_消息_格式化程序pretty_sysout=
新Kdu_消息_格式化程序(sysout);//非格式化打印机
Kdu_消息_格式化程序pretty_syserr=
新Kdu_消息_格式化程序(syserr);//使用格式化打印机
Kdu_global.Kdu_customize_警告(pretty_sysout);
Kdu_global.Kdu_customize_错误(pretty_syserr);
Kdu_simple_file_source raw_src=null;//必须最后处理
Jp2_family_src family_src=新的Jp2_family_src();//最后处置
Jpx_source wrapped_src=new Jpx_source();//在码流之前处理
Kdu_codestream codestream=新的Kdu_codestream();//需要“销毁”
Kdu_channel_mapping channels=新的Kdu_channel_mapping();
Kdu_region_decompressor decompressor=新Kdu_region_decompressor();
尝试
{ 
//以原始码流或JP2/JPX文件的形式打开输入文件
字符串fname=Environment.getExternalStorageDirectory().getPath()+“/Android/CB.jp2”;
family_src.Open(fname);//如果文件不存在,则生成错误
Jpx_layer_source xlayer=null;
Jpx_codestream_source xstream=null;
int success=wrapped\u src.Open(family\u src,true);
Kdu_compressed_source input=null;//允许我们引用压缩的
//与原始代码流关联的数据源
//文件或JP2/JPX嵌入的码流。
如果(成功<0)
{//必须作为原始文件打开
族_src.Close();
wrapped_src.Close();
raw_src=新Kdu_简单文件_源(fname);
输入=原始\u src;
}
其他的
{//已成功打开包装的JP2/JPX源
xlayer=wrapped\u src.Access\u层(0);
xstream=wrapped_src.Access_码流(xlayer.Get_码流_id(0));
input=xstream.Open_stream();
}
//创建代码流管理机制
创建(输入);
如果(xlayer!=null)
channels.Configure(xlayer.Access_color(0),xlayer.Access_channels(),
xstream.Get_codestream_id(),
xstream.Access_调色板(),
xstream.Access_dimensions());
其他的
配置(码流);
int ref_component=channels.Get_source_component(0);
Kdu坐标参考扩展=
确定参考扩展(参考组件、信道、码流);
//确定渲染结果的维度并启动解压缩程序
Kdu dims视图dims=
解压器。获取渲染图像(码流,通道,-1,
0,参考扩展,新Kdu坐标(1,1),
Kdu_global.Kdu_WANT_OUTPUT_COMPONENTS);
Kdu_coords view_size=视图_dims.Access_size();
//注意:“视图大小”的更改也会影响“视图大小”
//开始显示(查看大小);
Kdu坐标显示大小=
新Kdu_coords(200200);
if(查看大小。获取大小()>显示大小。获取大小())
查看大小。设置大小(显示大小。获取大小();
if(查看大小。获取大小()>显示大小。获取大小())
查看大小。设置大小(显示大小。获取大小());
解压器启动(码流,通道,-1,016384,视图尺寸,
参考扩展,新Kdu坐标(1,1),错误,
Kdu_global.Kdu_WANT_OUTPUT_COMPONENTS);
//增量渲染。
int region_buf_size=view_size.Get_x()*60;
int[]区域大小=新的int[区域大小];
Kdu_dims new_region=新Kdu_dims();
Kdu_dims不完整_区域=新Kdu_dims();
区域不完整。分配(视图尺寸);
while(decompressor.Process(region_buf,view_dims.Access_pos()),
public class MainActivity extends ActionBarActivity implements OnClickListener {
    Button btn;
    ImageView view;
    Bitmap bm;
    Canvas canvas;
    Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) super.findViewById(R.id.button1);
        btn.setOnClickListener(this);

        view = (ImageView) super.findViewById(R.id.imageView1);
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        bm = Bitmap.createBitmap(200, 200, conf); 
        Canvas canvas = new Canvas(bm);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(50, 50, 10, paint);
        view.setImageBitmap(bm);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        try 
        {
          // Start by customizing the error and warning services in Kakadu
          Kdu_sysout_message sysout =
            new Kdu_sysout_message(false); // Non-throwing message printer
          Kdu_sysout_message syserr =
            new Kdu_sysout_message(true); // Exception-throwing message printer
          Kdu_message_formatter pretty_sysout =
            new Kdu_message_formatter(sysout); // Non-throwing formatted printer
          Kdu_message_formatter pretty_syserr =
            new Kdu_message_formatter(syserr); // Throwing formatted printer

          Kdu_global.Kdu_customize_warnings(pretty_sysout);
          Kdu_global.Kdu_customize_errors(pretty_syserr);
          Kdu_simple_file_source raw_src = null; // Must be disposed last
          Jp2_family_src family_src = new Jp2_family_src(); // Dispose last
          Jpx_source wrapped_src = new Jpx_source(); // Dispose before codestream
          Kdu_codestream codestream = new Kdu_codestream(); // Needs `destroy'
          Kdu_channel_mapping channels = new Kdu_channel_mapping();
          Kdu_region_decompressor decompressor = new Kdu_region_decompressor();

          try 
          { 
            // Open input file as raw codestream or a JP2/JPX file
            String fname = Environment.getExternalStorageDirectory().getPath() + "/Android/CB.jp2";
            family_src.Open(fname); // Generates an error if file doesn't exist
            Jpx_layer_source xlayer = null;
            Jpx_codestream_source xstream = null;
            int success = wrapped_src.Open(family_src,true);
            Kdu_compressed_source input=null; // Allows us to refer to compressed
            // data source associated with either a raw codestream
            // file or a JP2/JPX embedded codestream.
            if (success < 0)
            { // Must open as raw file
              family_src.Close();
              wrapped_src.Close();
              raw_src = new Kdu_simple_file_source(fname);
              input = raw_src;
            }
            else
            { // Succeeded in opening as wrapped JP2/JPX source
              xlayer = wrapped_src.Access_layer(0);
              xstream = wrapped_src.Access_codestream(xlayer.Get_codestream_id(0));
              input = xstream.Open_stream();
            }
         // Create the code-stream management machinery
            codestream.Create(input);
            if (xlayer != null)
              channels.Configure(xlayer.Access_colour(0),xlayer.Access_channels(),
                                 xstream.Get_codestream_id(),
                                 xstream.Access_palette(),
                                 xstream.Access_dimensions());
            else
              channels.Configure(codestream);
            int ref_component = channels.Get_source_component(0);
            Kdu_coords ref_expansion =
              determine_reference_expansion(ref_component,channels,codestream);

            // Determine dimensions for the rendered result & start decompressor
            Kdu_dims view_dims =
              decompressor.Get_rendered_image_dims(codestream,channels,-1,
                                        0,ref_expansion,new Kdu_coords(1,1),
                                        Kdu_global.KDU_WANT_OUTPUT_COMPONENTS);
            Kdu_coords view_size = view_dims.Access_size();
                // Note: changes in `view_size' will also affect `view_dims'
            //start_display(view_size);
            Kdu_coords display_size =
              new Kdu_coords(200,200);
            if (view_size.Get_x() > display_size.Get_x())
              view_size.Set_x(display_size.Get_x());
            if (view_size.Get_y() > display_size.Get_y())
              view_size.Set_y(display_size.Get_y());
            decompressor.Start(codestream,channels,-1,0,16384,view_dims,
                               ref_expansion,new Kdu_coords(1,1),false,
                               Kdu_global.KDU_WANT_OUTPUT_COMPONENTS);

            // Render incrementally.
            int region_buf_size = view_size.Get_x() * 60;
            int[] region_buf = new int[region_buf_size];
            Kdu_dims new_region = new Kdu_dims();
            Kdu_dims incomplete_region = new Kdu_dims();
            incomplete_region.Assign(view_dims);
            while (decompressor.Process(region_buf,view_dims.Access_pos(),
                                        0,0,region_buf_size,
                                        incomplete_region,new_region))
            { // Transfer decompressed region into the main buffer
              Kdu_coords offset =
                new_region.Access_pos().Minus(view_dims.Access_pos());
              Log.d("OFFSET", offset.Get_x() + "," + offset.Get_y());
              Log.d("COLOR CODES", "" + region_buf[0] + region_buf[1] + region_buf[2] + region_buf[3]);
              put_region(view_size.Get_x(),view_size.Get_y(),
                                 new_region.Access_size().Get_x(),
                                 new_region.Access_size().Get_y(),
                                 offset.Get_x(),offset.Get_y(),region_buf);
            }
            decompressor.Finish();
            //display.repaint();
          }
          catch (KduException e)
          { // See END NOTE 2
            System.out.println("[Caught exception \"" + e.getMessage() +
                               "\" -- code " +
                               Integer.toHexString(e.Get_kdu_exception_code()) +
                               "]");
          }
          // Cleanup: Disposal must happen in the right order
          // See END NOTE 3 for a discussion of Kakadu object disposal
          decompressor.Native_destroy();
          channels.Native_destroy();
          if (codestream.Exists()) codestream.Destroy();
          if (raw_src != null) raw_src.Native_destroy();
          wrapped_src.Native_destroy();
          family_src.Native_destroy();
        }
        catch (KduException e)
        { // See END NOTE 2
          System.out.println("[Caught exception during creation of key objects!]");
        }
    }

    private static Kdu_coords determine_reference_expansion(int reference_component, Kdu_channel_mapping channels, Kdu_codestream codestream) throws KduException{
        int c;
        Kdu_coords ref_subs = new Kdu_coords();
        Kdu_coords subs = new Kdu_coords();
        codestream.Get_subsampling(reference_component, ref_subs);
        Kdu_coords min_subs = new Kdu_coords();
        min_subs.Assign(ref_subs);
        for(c = 0; c < channels.Get_num_channels(); c++){
            codestream.Get_subsampling(channels.Get_source_component(c), subs);
            if(subs.Get_x() < min_subs.Get_x())
                min_subs.Set_x(subs.Get_x());
            if (subs.Get_y() < min_subs.Get_y())
                min_subs.Set_y(subs.Get_y());
        }

        Kdu_coords expansion = new Kdu_coords();
        expansion.Set_x(ref_subs.Get_x() / min_subs.Get_x());
        expansion.Set_y(ref_subs.Get_y() / min_subs.Get_y());
        for (c=0; c < channels.Get_num_channels(); c++)
          {
            codestream.Get_subsampling(channels.Get_source_component(c),subs);
            if ((((subs.Get_x() * expansion.Get_x()) % ref_subs.Get_x()) != 0) ||
                (((subs.Get_y() * expansion.Get_y()) % ref_subs.Get_y()) != 0))
              {
                Kdu_global.Kdu_print_error(
                  "The supplied JP2 file contains colour channels " +
                  "whose sub-sampling factors are not integer " +
                  "multiples of one another.");
                codestream.Apply_input_restrictions(0,1,0,0,null,
                                       Kdu_global.KDU_WANT_OUTPUT_COMPONENTS);
                channels.Configure(codestream);
                expansion = new Kdu_coords(1,1);
              }
          }
        return expansion;
    }



    class Kdu_sysout_message extends Kdu_message
    {
      public Kdu_sysout_message(boolean raise_exception)
      {
        this.raise_exception_on_end_of_message = raise_exception;
      }
      public void Put_text(String text)
      { // Implements the C++ callback function `kdu_message::put_text'
        System.out.print(text);
      }
      public void Flush(boolean end_of_message) throws KduException
      { // Implements the C++ callback function `kdu_message::flush'.
        if (end_of_message && raise_exception_on_end_of_message)
        throw new KduException(Kdu_global.KDU_ERROR_EXCEPTION,
                                   "In `Kdu_sysout_message'.");
      }
      private boolean raise_exception_on_end_of_message;
    }

    public void put_region(int view_width, int view_height, int reg_width, int reg_height, int reg_off_x, int reg_off_y, int[] reg_buf){
        int[] img_buf;
        if (bm == null){
            img_buf = new int[view_width*view_height];
        }

        int dest_idx = reg_off_x + reg_off_y*view_width;
        int src_idx = 0;
        int extra_row_gap = view_width - reg_width;
        int i, j;
        for (j=0; j < reg_height; j++, dest_idx+=extra_row_gap){
        for (i=0; i < reg_width; i++, src_idx++, dest_idx++){
            //Log.d("Test", i + "," + "," + j + "," + reg_buf[src_idx]);
            //bm.setPixel(i, j, Color.rgb(45, 127, 0));
            paint.setColor(reg_buf[src_idx]);
            Log.d("Paint", paint.toString());
            canvas.drawPoint(i, j, paint);
        }
        }//for
    }
}
canvas.drawPoint(i, j, paint);
Canvas canvas = new Canvas(bm);
canvas = new Canvas(bm);