Breaking News
Loading...
Tuesday 2 October 2012

Copy Bitmap using Bitmap.createBitmap()

03:05
To copy Bitmap, it's easy by calling static methods Bitmap.createBitmap().

Example to copy the center part of Bitmap:

Copy Bitmap using Bitmap.createBitmap()

package com.example.androidcolor;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView imgSource, imgTarget;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgSource = (ImageView)findViewById(R.id.imgsource);
imgTarget = (ImageView)findViewById(R.id.imgtarget);

//Load bitmap from internet
String onLineImgSource = "http://goo.gl/yxNeG";
URL urlImgSource;

try {
urlImgSource = new URL(onLineImgSource);
new MyNetworkTask(imgSource, imgTarget)
.execute(urlImgSource);
} catch (MalformedURLException e) {
e.printStackTrace();
}

}

private class MyNetworkTask extends AsyncTask<URL, Void, Bitmap>{

ImageView ivSource, ivTarget;

public MyNetworkTask(ImageView iSource, ImageView iTarget){
ivSource = iSource;
ivTarget = iTarget;
}

@Override
protected Bitmap doInBackground(URL... urls) {
Bitmap networkBitmap = null;

URL networkUrl = urls[0]; //Load the first element
try {
networkBitmap = BitmapFactory.decodeStream(
networkUrl.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}

return networkBitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
ivSource.setImageBitmap(result);
ivTarget.setImageBitmap(copyBitmap(result));
}

}

private Bitmap copyBitmap(Bitmap src){

//Copy the whole bitmap
//Bitmap newBitmap = Bitmap.createBitmap(src);

//Copy the center part only
int w = src.getWidth();
int h = src.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(src, w/4, h/4, w/2, h/2);

return newBitmap;
}

}


<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" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imgsource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imgtarget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

</LinearLayout>


Remark: Permission of "android.permission.INTERNET" is needed in this exercise to load Bitmap from internet.

download filesDownload the files.

Next:
- Example of using colorToHSV() and HSVToColor(), convert between argb color and HSV components.


0 comments:

Post a Comment

 
Toggle Footer