마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

android crup draw on 이 안그려 지고 에러로 꺼지는 이유를 모르겠습니다.

0 추천
package com.example.canvas;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class CropArea extends ImageView {
 private static final String TAG = "Crop_Image";
 float sx, ex, sy, ey;
 static int DEP = 30; // Crop 경계선의 유효폭(선근처)

// CropImageActivity cnxt;
 Bitmap bitmap;
 float mWidth;
 float mHeight;
 Paint pnt;

 Bitmap hBmp; // 선 가운데의 세로선택 아이콘
 Bitmap wBmp; // 가로

// private String outFilePath = niceheeUtil.strFilePath;
 
 public CropArea(Context context)
    {
        super(context);
        setBackgroundColor(0xFFFFFF);
    }

 public CropArea(Context context, AttributeSet attrs) {
  super(context, attrs);
  
  Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  mWidth = display.getWidth(); // 화면 크기 설정
  mHeight = display.getHeight();

  sx = mWidth / 5; // 초기 Crop선의 위치 설정
  ex = mWidth * 4 / 5;
  sy = mHeight / 5;
  ey = mHeight * 4 / 5;

//  Log.e(TAG, outFilePath);
//  cnxt = (CropImageActivity) context;

  // 비트맵 크기 조절(메모리 문제로 인하여 1/2 크기로)
  BitmapFactory.Options resizeOpts = new Options();
  resizeOpts.inSampleSize = 2;
  try {
//   bitmap = BitmapFactory.decodeStream(
//     new FileInputStream(outFilePath), null, resizeOpts);
  } catch (Exception e) {
   e.printStackTrace();
  }

  bitmap = Bitmap.createScaledBitmap(bitmap, (int) mWidth, (int) mHeight,
    false);
  Log.e(TAG, "" + bitmap.getHeight() * bitmap.getWidth());
  hBmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.camera_crop_height);
  wBmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.camera_crop_width);

  // 페인트 설정
  pnt = new Paint();
  pnt.setColor(Color.MAGENTA);
  pnt.setStrokeWidth(3);
 }

 public void onDraw(Canvas canvas) {
  // 사각형 라인 그리기
  canvas.drawBitmap(bitmap, 0, 0, null);
  canvas.drawLine(sx, sy, ex, sy, pnt);
  canvas.drawLine(ex, sy, ex, ey, pnt);
  canvas.drawLine(sx, sy, sx, ey, pnt);
  canvas.drawLine(sx, ey, ex, ey, pnt);
  // 상하좌우 버튼들
  canvas.drawBitmap(hBmp, (ex + sx) / 2 - 19, sy - 19, null); // 폭이 38이므로
                 // 그려줄 좌상단
                 // 위치 지정
  canvas.drawBitmap(hBmp, (ex + sx) / 2 - 19, ey - 19, null);
  canvas.drawBitmap(wBmp, sx - 19, (ey + sy) / 2 - 19, null);
  canvas.drawBitmap(wBmp, ex - 19, (ey + sy) / 2 - 19, null);
 }

 // 이벤트 처리, 현재의 그리기 모드에 따른 점의 위치를 조정
 float dx = 0, dy = 0;
 float oldx, oldy;
 boolean bsx, bsy, bex, bey;
 boolean bMove = false;

 public boolean onTouchEvent(MotionEvent e) {
  int x = (int) e.getX();
  int y = (int) e.getY();

  if (e.getAction() == MotionEvent.ACTION_DOWN) {
   oldx = x;
   oldy = y;

   // 눌려진곳이 선 근처인가 확인
   if ((x > sx - DEP) && (x < sx + DEP))
    bsx = true;
   else if ((x > ex - DEP) && (x < ex + DEP))
    bex = true;

   if ((y > sy - DEP) && (y < sy + DEP))
    bsy = true;
   else if ((y > ey - DEP) && (y < ey + DEP))
    bey = true;

   // 어느 하나라도 선택이 되었다면 move에서 값 변경
   if ((bsx || bex || bsy || bey))
    bMove = false;
   else if (((x > sx + DEP) && (x < ex - DEP))
     && ((y > sy + DEP) && (y < ey - DEP)))
    bMove = true;

   return true;
  }

  if (e.getAction() == MotionEvent.ACTION_MOVE) {
   if (bsx)
    sx = x;
   if (bex)
    ex = x;
   if (bsy)
    sy = y;
   if (bey)
    ey = y;

   // 사각형의 시작 라인보다 끝라인이 크지않게 처리
   if (ex <= sx + DEP) {
    ex = sx + DEP;
    return true;
   }
   if (ey <= sy + DEP) {
    ey = sy + DEP;
    return true;
   }

   // 움직인 거리 구해서 적용
   if (bMove) {
    dx = oldx - x;
    dy = oldy - y;

    sx -= dx;
    ex -= dx;
    sy -= dy;
    ey -= dy;

    // 화면밖으로 나가지않게 처리
    if (sx <= 0)
     sx = 0;
    if (ex >= mWidth)
     ex = mWidth - 1;

    if (sy <= 0)
     sy = 0;
    if (ey >= mHeight)
     ey = mHeight - 1;
   }

   invalidate(); // 움직일때 다시 그려줌
   oldx = x;
   oldy = y;
   return true;
  }

  // ACTION_UP 이면 그리기 종료
  if (e.getAction() == MotionEvent.ACTION_UP) {
   bsx = bex = bsy = bey = bMove = false;
   return true;
  }
  return false;
 }

 // 선택된 사각형의 이미지를 저장
 public void save() {
  Bitmap tmp = Bitmap.createBitmap(bitmap, (int) sx, (int) sy,
    (int) (ex - sx), (int) (ey - sy));
  byte[] byteArray = bitmapToByteArray(tmp);
//  File file = new File(outFilePath);
//  Log.e("nicehee", file.getAbsolutePath());
  try {
//   FileOutputStream fos = new FileOutputStream(file);
//   fos.write(byteArray);
//   fos.flush();
//   fos.close();
  } catch (Exception e) {
//   Toast.makeText(cnxt, "파일 저장 중 에러 발생 : " + e.getMessage(), 0).show();
   return;
  }
 }

 // 이미지를 전송하기위한 테스트 코드
 public byte[] bitmapToByteArray(Bitmap bitmap) {
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bitmap.compress(CompressFormat.JPEG, 100, stream);
  byte[] byteArray = stream.toByteArray();
  return byteArray;
 }
}

나머진 다 기본 정보입니다. DrawOn 만 뿌려주고 확인하고 싶은데 확인할 방법이 없습니다.

unfortunately, project has stopped ... 에러만 뜹니다.

익명사용자 님이 2015년 5월 19일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...