zoukankan      html  css  js  c++  java
  • Bitmap对图像的处理

                                                               Bitmap对图像的处理

    一、引言:

          在开发中涉及到图片包括.png,.gif,.9.png,.jpgDrawable系对象,以及位图Bitmap,那么Bitmap是一个什么角色,如下将做一个详细介绍.

    二、Bitmap概述

      Bitmap(位图图像), 亦称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的.扩展名可以是.bmp或者.dib。它将图像定义为由点(像素)组成,每个点可以由多种色彩表示,包括248162432位色彩。例如,一幅1024×768分辨率的32位真彩图片,其所占存储字节数为:1024×768×32/8=3072KB,虽然位图文件图像效果好,但是非压缩格式的,需要占用较大存储空间,不利于在网络上传送Android系统当中,Bitmap是图像处理最重要的中转类之一。用它可以获取图像信息,借助Matrix对图像进行剪切、旋转、缩放等操作,同时还可以指定格式和压缩质量保存图像文件。

     

    三、构造Bitmap对象

      Bitmap继承Parcelable,实现在android.graphics包中,是一个可以跨进程传输的对象。但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化。AndroidBitmap是采用了工厂的设计模式进行获取此对象.

      1Bitmap静态方法static Bitmap createBitmap()

     public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight,boolean filter)//对源位图src缩放成宽为w,高为h的新位图

     public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height,@Nullable Matrix m, boolean filter)//从源位图src的指定坐标(x,y)开始,截取宽w,高h的部分,按照Matrix变换创建新的位图对象

     public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height,@NonNull Config config, boolean hasAlpha, @NonNull ColorSpace colorSpace) //从原位图获取到一个密度变化的bitmap

     

      2、通过BitmapFactory工厂类的static Bitmap decodeXxx()

    decodeByteArray(byte[] data, int offset, int length) 从指定字节数组的offset位置开始,将长度为length的数据解析成位图

    decodeFile(String pathName) pathName对应的文件解析成的位图对象

    decodeFileDescriptor(FileDescriptor fd) FileDescriptor中解析成的位图对象

    decodeResource(Resource res,int id) 根据给定的资源Id解析成位图

    decodeStream(InputStream in) 把输入流解析成位图

     

     

    三、BitmapMatrix一起使用

      以下将列举一些例子进行说明.

      Activity的code如下:

    package com.example.mytest;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
         private ImageView mImageView0;
         private ImageView mImageView1;
         private ImageView mImageView2;
         private ImageView mImageView3;
         private ImageView mImageView4;
         private ImageView mImageView5;
         private TextView mTextView;
         private Button left,right;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mImageView0 = (ImageView)findViewById(R.id.image0);
            mImageView1 = (ImageView)findViewById(R.id.image1);
            mImageView2 = (ImageView)findViewById(R.id.image2);
            mImageView3 = (ImageView)findViewById(R.id.image3);
            mImageView4 = (ImageView)findViewById(R.id.image4);
            mImageView5 = (ImageView)findViewById(R.id.image5);
            mTextView = (TextView)findViewById(R.id.textview);
            left=(Button)findViewById(R.id.left);
            left.setText("向左转");        
            right=(Button)findViewById(R.id.right);
            right.setText("向右转");    
            final Bitmap bmp1=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher1); //resources资源里
            final int width=bmp1.getWidth();
            final int height=bmp1.getHeight();
            Log.d("TEST","  "+ width +"  height: "+height);
            mImageView1.setImageBitmap(bmp1);//通过Bitmap将图片放入ImageView中显示出来
    
              //缩小为原来一半
             Matrix matrix = new Matrix();
             matrix.setScale(0.5f, 0.5f);//缩小
             Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(),  
                    bmp1.getHeight(), matrix, true);
             mImageView2.setImageBitmap(bmp2);
             //旋转
             matrix.postRotate(45.0f);// 旋转45度 == matrix.setSinCos(0.5f, 0.5f); 
           Bitmap bmp3 = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(),  
                        bmp1.getHeight(), matrix, true);
             mImageView3.setImageBitmap(bmp3);
             //平移
             Matrix matrix1 = new Matrix();
             matrix1.setTranslate(bmp1.getWidth()*30, bmp1.getHeight()*30);// 向左下平移 
             Bitmap bmp4 = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(),  
                        bmp1.getHeight(), matrix1, true);
             mImageView4.setImageBitmap(bmp4);
             
             //斜切
             Matrix matrix2 = new Matrix();  
             matrix2.setSkew(0.5f, 0.5f);// 斜切  
             matrix2.postScale(0.5f, 0.5f);// 缩小为原来的一半  
             Bitmap bmp5 = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(),  
                     bmp1.getHeight(), matrix2, true);  
             mImageView5.setImageBitmap(bmp5); 
        
     
            left.setOnClickListener(new OnClickListener(){
                 @Override
                 public void onClick(View v) {
                     Matrix matrix = new Matrix();
                     matrix.setScale(0.5f, 0.5f);
                     Bitmap bmp5 = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(),  
                             bmp1.getHeight(), matrix, true);
                     mImageView3.setImageBitmap(bmp5);
                     showToast(matrix);
                 }
            });
    
        }
        void showToast(Matrix matrix) {  
            String string = "";  
            float[] values = new float[9];  
            matrix.getValues(values);  
            for (int i = 0; i < values.length; i++) {  
                string += "matrix.at" + i + "=" + values[i];  
            }  
            Toast.makeText(this, string, Toast. LENGTH_LONG).show();  
            Log.d("TEST"," showToast:" + string);  
        } 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }

      布局文件如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
       <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
       <Button
        android:id="@+id/left"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
        <Button
        android:id="@+id/right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
        <ImageView
        android:id="@+id/image0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/btn_fm_list_favorite_on"
        />
        <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/btn_fm_list_favorite_on"
        />
       <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/btn_fm_list_favorite_on"
        />
       <ImageView
        android:id="@+id/image3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/btn_fm_list_favorite_on"
        />    
      <ImageView
        android:id="@+id/image4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/btn_fm_list_favorite_on"
        /> 
       <ImageView
        android:id="@+id/image5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/btn_fm_list_favorite_on"
        />      
    </LinearLayout>

    对应的效果如下(本处只做效果说明,在UI显示上有失美观,请勿喷,后续改进)

    第一个五星是从资源库中取到的图,纵向排列第二个是通过Bitmap将从资源图片放入mImageView1中显示出来.按照代码图片显示依次类推.

    问题点:平移好像没变,本地有试过将平移距离变大平移不显示,不知道是肉眼的问题还是逻辑哪里有问题.

    如有问题请大家指出,以及需要探讨的地方也请指出,一起探讨进步.


  • 相关阅读:
    jquery-validate v1.19.2 源码分析
    jquery之遍历-Section04
    jquery之元素-Section03
    jquery之效果-Section02
    jquery之入门-Section01
    CSS世界(七)思维导图
    shell文件处理awk
    jquery插件懒加载
    jquery插件改变背景色
    jquery多库共存
  • 原文地址:https://www.cnblogs.com/syyh2006/p/9035648.html
Copyright © 2011-2022 走看看