android影像編輯和處理(一)

來源:互聯網
上載者:User

android影像編輯和處理(一)

1.使用內建Gallery應用程式選擇映像:

 


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;
try {

 

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();}}}}

 

 

2.在位元影像上繪製位元影像

 

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);
             

註:有什麼不懂的可以留言。。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.