zoukankan      html  css  js  c++  java
  • 4.2.3 保存基于位图的画布绘图

        如果仅仅是在图像上绘制,而不能再用户创建一幅杰作之后对其进行保存,那么这将毫无意义,迄今为止,我们已经绘制了图像——接下来看看如何对这些奇妙的绘图进行持久保存。至少看看如何将他们保存到SD卡中。

        毫无疑问,这与在第2章的自定义摄像头应用程序中保存捕获图像的过程类似。让我们了解一下为保存图像而对ChoosePictureDraw示例所做的改动。

        在onCreate方法中获得新按钮的引用。我们会将该按钮添加到布局XML中,并在活动中声明它。

        在定义了类之后,声明saveButton按钮以及其他的实例变量。

    1  private Button saveButton;

       在onCreate中获得choosePicture按钮的引用之后,我们将获得saveButton按钮的引用:

    1 saveButton=(Button) findViewById(R.id.SavePictureButton);

        随后,将该按钮的OnClickListener设置为当前活动,对choosePicture按钮进行相同的处理。

    1 saveButton=(Button) findViewById(R.id.SavePictureButton);

        现在,考虑到这两个不同的按钮都会使用onClick方法,因此需要对该方法进行修改。最简单的方法是将传入的View对象与按钮进行比较。如果该View对象等于某个按钮,它就是被单击的按钮。

     1     @Override
     2     public void onClick(View v) {
     3         if(v==choosePicture){
     4              Intent choosePictureIntent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     5               startActivityForResult(choosePictureIntent, 0);
     6         }else if(v==saveButton){
     7             if(alteredBitmap!=null){
     8                 Uri imageFileUri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
     9                 try{
    10                     OutputStream imageFileOS=getContentResolver().openOutputStream(imageFileUri);
    11                     alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);
    12                 }catch(Exception e){
    13                     
    14                 }
    15             }
    16         }
    17     }

        知道单击的是saveButton按钮之后,需要确保已经定义了正用于绘制的位图图像。

    1                if(alteredBitmap!=null){

        完成上述的操作后,我们就准备好保存图像。与之前的示例一样,可以查询MediaStore,获取新图像的Uri,并根据该Uri创建OutputStream。

    1                Uri imageFileUri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
    2                 try{
    3                     OutputStream imageFileOS=getContentResolver().openOutputStream(imageFileUri);

       在此之前的“保存图像”的示例中,我们实际上已经获得了JPEG形式的数据,因此只须简单的将其写出到OutputStream。在当前的情况下,必须使用该位图对象的compress方法将其转换成JPEG图像(或者也可以选择PNG格式)。

        compress方法压缩位图数据。并将其写出到OutputStream。它会采用在Bitmap.CompressFormat中定义的常量,即PNG或JPEG。PNG非常适合艺术线条和图形,而JPEG非常适合带渐变的全彩图像,例如照片。由于在这里使用的是照片,因此将采用JPEG格式。

        下一个参数是质量设置。只有当压缩为JPEG图像时,质量设置才是有用的,因为PNG图像将始终保持所有数据,从而使质量设置无效。JPEG就是所谓的“有损的”编解码器,这意味着将会丢弃数据。质量与大小成反比关系。质量设置为0将产生一个很小的文件大小,但是图像不太美观;质量设置为100将产生一个很大的文件大小,且图像看起来非常美观。这个示例使用质量设置90,这是某种折衷选择。

        需要传入的最后一个参数是实际写入文件的OutputStream。

    1                     alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);
    2                 }catch(Exception e){
    3                     
    4                 }
    5             }
    6         }
    7     }

        下面是更新的布局XML,其中包含了新的Save按钮。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical"
     5     >
     6     <Button 
     7         android:id="@+id/ChoosePictureButton"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:text="Choose Picture"/>
    11     <ImageView  
    12         android:id="@+id/ChooseImageView"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:contentDescription="@string/app_name"/>
    16     <Button 
    17         android:id="@+id/SavePictureButton"
    18         android:layout_width="fill_parent"
    19         android:layout_height="wrap_content"
    20         android:text="Save Picture"/>
    21 </LinearLayout>
  • 相关阅读:
    windows 录音程序(二)
    windows 录音程序(一)
    基于VC的ACM音频编程接口压缩Wave音频(三)
    基于VC的ACM音频编程接口压缩Wave音频(二)
    基于VC的ACM音频编程接口压缩Wave音频(一)
    基于VC的声音文件操作(五)
    基于VC的声音文件操作(四)
    2016年,随笔.
    golang 私钥"加密"公钥"解密"
    bt种子文件文件结构
  • 原文地址:https://www.cnblogs.com/ZSS-Android/p/3937788.html
Copyright © 2011-2022 走看看