Tag Archives: 안드로이드

[Java/Android] 간단하게 사용하는 Bitmap 리사이즈(Resize) 예제

안드로이드에서 간단하게 Bitmap 이미지를 리사이즈 할 수 있는 메소드를 기억 저장용으로 기록해 둡니다. 리사이즈 하고자 하는 비트맵 이미지와 긴축 최대 길이를 지정하면 긴축에 맞추어 비율에 맞게 리사이즈를 하는 메소드입니다.

/**
 * Bitmap이미지의 가로, 세로 사이즈를 리사이징 한다.
 * 
 * @param source 원본 Bitmap 객체
 * @param maxResolution 제한 해상도
 * @return 리사이즈된 이미지 Bitmap 객체
 */
public Bitmap resizeBitmapImage(Bitmap source, int maxResolution)
{
    int width = source.getWidth();
    int height = source.getHeight();
    int newWidth = width;
    int newHeight = height;
    float rate = 0.0f;

    if(width > height)
    {
        if(maxResolution < width)
        {
            rate = maxResolution / (float) width;
            newHeight = (int) (height * rate);
            newWidth = maxResolution;
        }
    }
    else
    {
        if(maxResolution < height)
        {
            rate = maxResolution / (float) height;
            newWidth = (int) (width * rate);
            newHeight = maxResolution;
        }
    }

    return Bitmap.createScaledBitmap(source, newWidth, newHeight, true);
}

 

[QR코드리더] ZXing을 이용하여 내 안드로이드 어플에 QR코드 스캐너 도입하기

[ZXING]이라는 정말 잘 만들어진 안드로이드에서도 사용할 수 있는 QR코드/바코드 스캐너가 있습니다. 아이폰에서는 적당히 자유롭게 쓸 수 있지만 안드로이드에서는 Intent라는 좋은(?) 개념때문인지 Barcode Scanner라는 프로그램을 설치하여 그것을 Intent로 호출해 사용하게끔 하고 있더군요.

그리하여 ZXing의 안드로이드 소스를 커스터마이징 해서 그냥 사용할 수 있도록 약간 고쳐보았습니다. 만들어진 소스는 ZXing android + android-integration을 통합하여 제작하였습니다.

사용하시는 방법은 소스코드를 그냥 통채로 올리니 참고하시면 되겟지만 간단하게 기록을 해보겠습니다.

1. AndroidManifest.xml에 다음을 추가합니다.

[code]<activity
  android:name=”com.google.zxing.client.android.CaptureActivity”
  android:screenOrientation=”landscape”
  android:configChanges=”orientation|keyboardHidden”
  android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
  android:windowSoftInputMode=”stateAlwaysHidden”>
    <intent-filter>
      <action android:name=”com.google.zxing.client.android.SCAN”/>
      <category android:name=”android.intent.category.DEFAULT”/>
    </intent-filter>
</activity>

<uses-feature android:name=”android.hardware.camera”/>
<uses-permission android:name=”android.permission.CAMERA”/>
<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.FLASHLIGHT”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>[/code]

2. 리소스 파일을 복사합니다. UI를 변경하고 싶을 경우 capture.xml을 변경하시면 됩니다.

res/layout/capture.xml
res/layout/ids.xml

3. 스캐너 호출 방법 (android-integration)

[code]// QR코드/바코드 스캐너를 구동합니다.
IntentIntegrator.initiateScan(MainActivity.this);[/code]

4. 결과 처리 방법

[code]protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
  // QR코드/바코드를 스캔한 결과 값을 가져옵니다.
  IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

  // 결과값 출력
  new AlertDialog.Builder(this)
  .setTitle(R.string.app_name)
  .setMessage(result.getContents() + ” [” + result.getFormatName() + “]”)
  .setPositiveButton(“확인”, new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
      dialog.dismiss();
    }
  })
  .show();
}[/code]

1394066880.zip