利用Bimap,canvas处理图片并保存(源码)

本文介绍如何在Android中利用Bimap和Canvas处理图片,包括在图片上画直线,并展示和保存处理后的图像。通过创建自定义DrawView类,实现触屏事件处理,实现在图片上绘制并保存到Bitmap。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android怎么样对一张图片处理,并保存好。

我从最简单的使用开始,在一张图片上画直线,然后显示这张画好的图。
首先要知道Bimap,canvas的用法, 它的详细用法大家可以网上去找资料,重要的是 我要说下canvas有个构造函数Canvas (Bitmap bitmap);我们对这个 canvas 的操作实际上就是操作它的Bitmap;
主Activity 的主要代码如下:   

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class DrawActivity extends Activity {
    static boolean drawFlag=false;
    private Button show_btn; //清除所画图像按键
    private Button line_btn;//开始画直线按钮
    private ImageView image;
    private DrawView drawView;
    private Bitmap bimap;
    
       /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        show_btn=(Button) findViewById(R.id.clear_btn);
        line_btn=(Button) findViewById(R.id.length_btn);
        line_btn.setOnClickListener(new ClickEvent());
        show_btn.setOnClickListener(new ClickEvent());
        image=(ImageView) findViewById(R.id.imageView1);
        drawView=(DrawView)findViewById(R.id.drawView);
        
        //如果不用copy的方法,直接引用会对资源文件进行修改,而android是不允许在代码里修改res文件里的图片
        bimap=BitmapFactory.decodeResource(getResources(), R.drawable.background).copy(Bitmap.Config.ARGB_8888, true);
        
    }
    
    class ClickEvent implements View.OnClickListener {
        public void onClick(View v) {
            if(v==show_btn){
                image.setImageBitmap(bimap);//显示图片
            }
          if(v==line_btn){
              drawView.setBitmap(bimap);//传我们需要处理的bimap给画图类
              DrawActivity.drawFlag=true; //开始画图的标志
            
            }
        }
    }

}


然后在在画图类的Ondraw()函数中处理图片,在图片上画直线"
   package com.android.draw;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class DrawView extends ImageView {
    private float beginX=0,beginY=0,endX=0,endY=0;//画直线的起点和终点的X和Y坐标
    private int eventflag = 0;//触屏事件点击
    private Canvas canvasself;//用于保存所画图像的画布
    private Bitmap bimap;//用于保存所画图像的图画
    
    public DrawView(Context context) {
        super(context);
    }
    
/*******
在XML中使用自定义View必须要使用含AttributeSet变量参数的构造函数
******/
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

   public void setBitmap(Bitmap b){
     bimap=b;
     canvasself = new Canvas(bimap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);    
        if(DrawActivity.drawFlag){
            Paint p=new Paint();
            /*********设置画笔********/
            p.setColor(Color.WHITE);
            p.setStyle(Paint.Style.STROKE);
            p.setAntiAlias(true);
            
            canvas.drawBitmap(bimap, 0, 0,null);//把之前画出来保存后的图再画出来
            canvas.drawLine(beginX, beginY, endX, endY, p);//画中间长得线段
            if(eventflag==3){
                //这里将直线画到canvasself上,它就保存在了bimap这张图上
                canvasself.drawLine(beginX, beginY, endX, endY, p);
            }        
        }
        
    }
    画图类的触屏事件处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        endX = event.getX();
        endY = event.getY();
        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            eventflag = 1;
            beginX = endX;
            beginY = endY;
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            eventflag = 2;
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            eventflag = 3;
            invalidate();
            break;
        }
        
        return true;
    }


main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">
    
           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
                    
                <Button android:id="@+id/length_btn"
                        android:layout_height="wrap_content"
                        android:text="画直线"
                        android:layout_width="wrap_content"/>
               <TextView android:text="画图区"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />    
                  
             <com.android.draw.DrawView
                      android:id="@+id/drawView"
                      android:layout_weight="1"
                    android:layout_height="fill_parent"
                     android:layout_width="fill_parent" >               
              </com.android.draw.DrawView>  


        </LinearLayout>
    
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
                  
        <Button android:id="@+id/clear_btn"
                android:text="显示"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
       <TextView android:text="显示画图区"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" />
       <ImageView android:id="@+id/imageView1"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="wrap_content" />    



    </LinearLayout>

</LinearLayout>

源码在我的这篇博客里有

http://www.eoeandroid.com/thread-114497-1-1.html



### Java 中 BiMap 的使用方法及实现 #### 什么是 BiMapBiMap 是一种特殊的映射结构,在其中键到值的映射是唯一的,反之亦然。这意味着不仅每个键都对应唯一的一个值,而且每个值也只对应一个键。这使得可以从键查找值,也可以从值反向查找键。 Guava 库提供了几种不同类型的 `BiMap` 实现: - **HashBiMap**: 基于哈希表实现的标准双射映射。 - **EnumBiMap**: 当键和值都是枚举类型时使用的高效版本。 - **EnumHashBiMap**: 键为枚举类型,值可以是非枚举类型的变体。 - **ImmutableBiMap**: 不可变的双射映射,适用于创建固定不变的映射实例[^2]。 #### 创建操作 BiMap 下面是一个简单的例子展示如何在 Java 中使用 Guava 提供的 `HashBiMap` 来存储和检索数据: ```java import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; public class BimapExample { public static void main(String[] args) { // 初始化一个新的 HashBiMap 对象 BiMap<Integer, String> biMap = HashBiMap.create(); // 向 BiMap 添加条目 biMap.put(1, "one"); biMap.put(2, "two"); System.out.println("Original mapping:"); for (Integer key : biMap.keySet()) { System.out.println(key + ": " + biMap.get(key)); } // 获取逆向视图 BiMap<String, Integer> inverse = biMap.inverse(); System.out.println("\nInverted mapping:"); for (String value : inverse.keySet()) { System.out.println(value + ": " + inverse.get(value)); } } } ``` 这段代码展示了如何初始化一个 `HashBiMap` 填充一些初始项;接着通过调用 `.inverse()` 方法获得其反转后的表示形式,从而允许基于值来访问对应的键[^1]。 当尝试插入重复的值时会抛出异常,因为违反了 BiMap 关系的一对一特性。例如试图再次添加 `"小小"` 这样的值将会引发错误。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值