android图像编辑和处理(一)
来源:互联网 发布:java调用其他类方法 编辑:程序博客网 时间:2024/05/22 05:26
1.使用内置Gallery应用程序选择图像:
</pre><pre name="code" class="java">package com.example.testphotoedit;import java.io.FileNotFoundException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.net.Uri;import android.os.Bundle;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity implements OnClickListener {private ImageView chosenImageView,copyPicture;private Button choosePicture;private Uri imageFileUri;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);choosePicture = (Button) findViewById(R.id.button_chose);chosenImageView = (ImageView) findViewById(R.id.chose_picture);copyPicture=(ImageView) findViewById(R.id.copy_picture);choosePicture.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v.getId() == R.id.button_chose) {Intent chooseIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//启动Gallery应用程序startActivityForResult(chooseIntent, 0);}}
/* (non-Javadoc) * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) * * 在返回的意图数据中,返回选择的图像的URI */@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {imageFileUri = data.getData();Display currentDisply = getWindowManager().getDefaultDisplay();int dw = currentDisply.getWidth() / 2 - 100;int dh = currentDisply.getHeight() / 2 - 100;
<span style="font-family: Arial, Helvetica, sans-serif;">try {</span>
BitmapFactory.Options bmpFactory = new BitmapFactory.Options();bmpFactory.inJustDecodeBounds = true;//加载图像的尺寸而非图像本身Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactory);int heightRatio = (int) Math.ceil(bmpFactory.outHeight/ (float) dh);int widthRatio = (int) Math.ceil(bmpFactory.outWidth/ (float) dw);if (heightRatio > 1 && widthRatio > 1) {if (heightRatio > widthRatio) {bmpFactory.inSampleSize = heightRatio;} else {bmpFactory.inSampleSize = widthRatio;}}bmpFactory.inJustDecodeBounds = false;//加载真实的图像bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactory);chosenImageView.setImageBitmap(bmp);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),bmp.getWidth(), bmp.getConfig());Canvas canvas=new Canvas(alteredBitmap);Paint paint=new Paint();canvas.drawBitmap(bmp, 0,0, paint);copyPicture.setImageBitmap(alteredBitmap);
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.testphotoedit.MainActivity$PlaceholderFragment" > <Button android:id="@+id/button_chose" android:layout_width="120dp" android:layout_height="60dp" android:text="选择图片"/> <ImageView android:id="@+id/chose_picture" android:layout_below="@+id/button_chose" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/copy_picture" android:layout_below="@+id/chose_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
注:有什么不懂的可以留言。。
0 0
- android图像编辑和处理(一)
- android图像编辑和处理(二)
- android图像编辑和处理(三)
- android图像编辑和处理(四)
- 【使用技巧】【图像编辑和处理】将GIF图缩放
- 【使用技巧】【图像编辑和处理】将多幅图片放到一块方便对比
- 泊松图像编辑(Poisson image editing)
- 泊松图像编辑(Poisson image editing)
- 泊松图像编辑(Poisson image editing)
- Android异步消息处理(一)--》Handler和多线程深入
- android绘图处理系列(一):bitmapShader和Xfermode
- Android 图片处理(一)
- android图像处理(一)
- Android图片处理(一)
- ubuntu下的截图和图像编辑软件推荐
- 基于Web的图像编辑实现方案(上)
- 基于Web的图像编辑实现方案(中)
- 图像编辑软件
- varchar2、nvarchar2
- [LeetCode] Reverse Integer
- 学习
- 用户信息的获取途径
- Google Gson 使用简介
- android图像编辑和处理(一)
- 秦安KinAn触摸屏8口LED KVM切换器DT1708—TEL:15035122463
- 虚拟继承体系下对象的大小
- 第一章——对象导论
- 逆波兰表达式
- 秦安KinAn17寸单口LED KVM切换器LS1701—TEL:15035122463
- Learning how to learn (Part 1)
- JQuery入门(1)
- P2P进化论 是成为恐龙还是成为达尔文雀?