Top Ad 728x90

More Stories

Sabtu, 21 September 2013

New Features in the Google Maps Mobile APIs for Android and iOS

by
Maps Live: New Features in the Google Maps Mobile APIs for Android and iOS

Google Maps API Team members Daniel Schramm and Chris Broadfoot discuss exciting new cross platform features in the Google Maps Android API v2 (http://goo.gl/5k18Es) and the Google Maps SDK for iOS (http://goo.gl/r2c2e). New to the latest versions are map padding, marker rotation, and flat markers.




The series:
A simple example using Google Maps Android API v2, step by step.

Kamis, 19 September 2013

Scale bitmap with inDither and inPreferQualityOverSpeed

by
The exercise "Load scaled bitmap" demonstrate a simple and standard method to scale bitmap. Additionally, we can set inDither and inPreferQualityOverSpeed options to improve quality of the scaled bitmap.
  • inDither:
    If set, the decoder will attempt to dither the decoded image.
    Dithering is a technique used in computer graphics to create the illusion of color depth in images with a limited color palette (color quantization). ~ reference: Wikipedia, Dither in Digital photography and image processing.
  • inPreferQualityOverSpeed (Added in API level 10) : 
    If set, the decoder will try to decode the reconstructed image to a higher quality even at the expense of the decoding speed. Currently the field only affects JPEG decode, in the case of which a more accurate, but slightly slower, IDCT method will be used instead.

In this exercise, we can set options of inDither and inPreferQualityOverSpeed in scaling bitmap. And also display the INACCURACY processing duration of scaling bitmap in millisecond, for reference only.

Scale bitmap with inDither and inPreferQualityOverSpeed

package com.example.androidscalebitmap;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {

final int RQS_IMAGE1 = 1;

Button btnLoadImage;
ToggleButton optBtnInDither, optBtnInPreferQualityOverSpeed;
TextView textInfo;
ImageView imageResult;

Uri source1;
Date startTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoadImage = (Button) findViewById(R.id.loadimage);
optBtnInDither = (ToggleButton) findViewById(R.id.optInDither);
optBtnInPreferQualityOverSpeed = (ToggleButton) findViewById(R.id.optInPreferQualityOverSpeed);
textInfo = (TextView) findViewById(R.id.info);
imageResult = (ImageView) findViewById(R.id.result);

btnLoadImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
}
});

optBtnInDither.setOnCheckedChangeListener(
optBtnOnCheckedChangeListener);
optBtnInPreferQualityOverSpeed.setOnCheckedChangeListener(
optBtnOnCheckedChangeListener);
}

OnCheckedChangeListener optBtnOnCheckedChangeListener =
new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
doScale(source1);
}};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
switch (requestCode) {
case RQS_IMAGE1:
source1 = data.getData();
optBtnInDither.setChecked(false);
optBtnInPreferQualityOverSpeed.setChecked(false);
doScale(source1);
break;
}
}
}

private void doScale(Uri src){

try {
Bitmap bm = loadScaledBitmap(source1);
imageResult.setImageBitmap(bm);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private Bitmap loadScaledBitmap(Uri src) throws FileNotFoundException {

// required max width/height
final int REQ_WIDTH = 800;
final int REQ_HEIGHT = 800;

Bitmap bm = null;

if (src != null) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver()
.openInputStream(src), null, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, REQ_WIDTH,
REQ_HEIGHT);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;

//set options according to RadioButton setting
options.inDither = optBtnInDither.isChecked();
//inPreferQualityOverSpeed require API Level 10
options.inPreferQualityOverSpeed = optBtnInPreferQualityOverSpeed.isChecked();

InputStream inputStream = getContentResolver().openInputStream(src);
startTime = new Date();

bm = BitmapFactory.decodeStream(inputStream, null, options);

//INACCURACY processing duration of scaling bitmap in millisecond
long duration = new Date().getTime() - startTime.getTime();
textInfo.setText("duration in ms: " + duration);
}

return bm;
}

public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will
// guarantee a final image with both dimensions larger than or
// equal to the requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}
}


<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"
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=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<Button
android:id="@+id/loadimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image 1" />
<ToggleButton
android:id="@+id/optInDither"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:textOn="inDither ON"
android:textOff="inDither OFF"/>
<ToggleButton
android:id="@+id/optInPreferQualityOverSpeed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:textOn="inPreferQualityOverSpeed ON"
android:textOff="inPreferQualityOverSpeed OFF"/>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:color/background_dark"
android:scaleType="center" />

</LinearLayout>


download filesDownload the files.


download filesDownload and try the APK.



more: Something about processing images in Android

Android Developer Tools Essentials: Android Studio to Zipalign

by

Android development can be challenging, but through the effective use of Android Developer Tools (ADT), you can make the process easier and improve the quality of your code. This concise guide demonstrates how to build apps with ADT for a device family that features several screen sizes, different hardware capabilities, and a varying number of resources.
With examples in Windows, Linux, and Mac OS X, you’ll learn how to set up an Android development environment and use ADT with the Eclipse IDE. Also, contributor Donn Felker introduces Android Studio, a Google IDE that will eventually replace Eclipse.
  • Learn how to use Eclipse and ADT together to develop Android code
  • Create emulators of various sizes and configurations to test your code
  • Master Eclipse tools, or explore the new Android Studio
  • Use Logcat, Lint, and other ADT tools to test and debug your code
  • Simulate real-world events, including location, sensors, and telephony
  • Create dynamic and efficient UIs, using Graphical Layout tools
  • Monitor and optimize you application performance using DDMS, HierarchyViewer, and the Android Monitor tool
  • Use Wizards and shortcuts to generate code and image assets
  • Compile and package Android code with Ant and Gradle

Rabu, 18 September 2013

Adjust HSV(Hue, Saturation and Value) of bitmap

by
android.graphics.Color class provide colorToHSV() and HSVToColor() methods to convert between ARGB color and HSV components. Such that we can adjust HSV of bitmap.

Adjust HSV(Hue, Saturation and Value) of bitmap

package com.example.androiddrawbitmap;

import java.io.FileNotFoundException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

Button btnLoadImage;
TextView textSource;
ImageView imageResult;
SeekBar hueBar, satBar, valBar;
TextView hueText, satText, valText;
Button btnResetHSV;

final int RQS_IMAGE1 = 1;

Uri source;
Bitmap bitmapMaster;
Canvas canvasMaster;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnLoadImage = (Button) findViewById(R.id.loadimage);
textSource = (TextView) findViewById(R.id.sourceuri);
imageResult = (ImageView) findViewById(R.id.result);

btnLoadImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
}
});

hueText = (TextView) findViewById(R.id.texthue);
satText = (TextView) findViewById(R.id.textsat);
valText = (TextView) findViewById(R.id.textval);
hueBar = (SeekBar) findViewById(R.id.huebar);
satBar = (SeekBar) findViewById(R.id.satbar);
valBar = (SeekBar) findViewById(R.id.valbar);
hueBar.setOnSeekBarChangeListener(seekBarChangeListener);
satBar.setOnSeekBarChangeListener(seekBarChangeListener);
valBar.setOnSeekBarChangeListener(seekBarChangeListener);
btnResetHSV = (Button)findViewById(R.id.resethsv);
btnResetHSV.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// reset SeekBars
hueBar.setProgress(256);
satBar.setProgress(256);
valBar.setProgress(256);

loadBitmapHSV();
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
switch (requestCode) {
case RQS_IMAGE1:
source = data.getData();

try {
bitmapMaster = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
source));

// reset SeekBars
hueBar.setProgress(256);
satBar.setProgress(256);
valBar.setProgress(256);

loadBitmapHSV();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

break;
}
}
}

OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener() {

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
loadBitmapHSV();
}

};

private void loadBitmapHSV() {
if (bitmapMaster != null) {

int progressHue = hueBar.getProgress() - 256;
int progressSat = satBar.getProgress() - 256;
int progressVal = valBar.getProgress() - 256;

/*
* Hue (0 .. 360) Saturation (0...1) Value (0...1)
*/

float hue = (float) progressHue * 360 / 256;
float sat = (float) progressSat / 256;
float val = (float) progressVal / 256;

hueText.setText("Hue: " + String.valueOf(hue));
satText.setText("Saturation: " + String.valueOf(sat));
valText.setText("Value: " + String.valueOf(val));

imageResult.setImageBitmap(updateHSV(bitmapMaster, hue, sat, val));

}
}

private Bitmap updateHSV(Bitmap src, float settingHue, float settingSat,
float settingVal) {

int w = src.getWidth();
int h = src.getHeight();
int[] mapSrcColor = new int[w * h];
int[] mapDestColor = new int[w * h];

float[] pixelHSV = new float[3];

src.getPixels(mapSrcColor, 0, w, 0, 0, w, h);

int index = 0;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {

// Convert from Color to HSV
Color.colorToHSV(mapSrcColor[index], pixelHSV);

// Adjust HSV
pixelHSV[0] = pixelHSV[0] + settingHue;
if (pixelHSV[0] < 0.0f) {
pixelHSV[0] = 0.0f;
} else if (pixelHSV[0] > 360.0f) {
pixelHSV[0] = 360.0f;
}

pixelHSV[1] = pixelHSV[1] + settingSat;
if (pixelHSV[1] < 0.0f) {
pixelHSV[1] = 0.0f;
} else if (pixelHSV[1] > 1.0f) {
pixelHSV[1] = 1.0f;
}

pixelHSV[2] = pixelHSV[2] + settingVal;
if (pixelHSV[2] < 0.0f) {
pixelHSV[2] = 0.0f;
} else if (pixelHSV[2] > 1.0f) {
pixelHSV[2] = 1.0f;
}

// Convert back from HSV to Color
mapDestColor[index] = Color.HSVToColor(pixelHSV);

index++;
}
}

return Bitmap.createBitmap(mapDestColor, w, h, Config.ARGB_8888);

}

}


<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"
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=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<Button
android:id="@+id/loadimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image 1" />

<TextView
android:id="@+id/sourceuri"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:color/background_dark"
android:scaleType="centerInside" />

<TextView
android:id="@+id/texthue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hue" />
<SeekBar
android:id="@+id/huebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="511"
android:progress="256"/>
<TextView
android:id="@+id/textsat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saturation" />
<SeekBar
android:id="@+id/satbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="511"
android:progress="256"/>
<TextView
android:id="@+id/textval"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Value" />
<SeekBar
android:id="@+id/valbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="511"
android:progress="256"/>
<Button
android:id="@+id/resethsv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset HSV" />
</LinearLayout>


download filesDownload the files.

download filesDownload and try APK.



more: Something about processing images in Android

Top Ad 728x90

Top Ad 728x90