2017-03-18 8 views
0

カメラからライブプレビューを取得し、ビットマップに変換してビューの中心からピクセルカラーを取得しています。私はview.getX()とview.getY()を試しましたが、常に0,0を返します。 getXとのgetYが、私は手動で値を入れているが、それは特定のデバイスになり働いていないので、ここでアンドロイドのビューの絶対座標を取得するにはどうすればいいですか?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<FrameLayout 
    android:id="@+id/camera_preview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 


<Button 
    android:id="@+id/button_capture" 
    android:text="Capture" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="onCapClick" 
    /> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv5" 
    android:layout_centerInParent="true" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv2" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@id/iv5" 
    android:layout_marginBottom="55dp" 
    android:background="@drawable/square"/> 


<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv8" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@id/iv5" 
    android:layout_marginTop="55dp" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv6" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@id/iv5" 
    android:layout_marginLeft="55dp" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv4" 
    android:layout_centerVertical="true" 
    android:layout_toLeftOf="@id/iv5" 
    android:layout_marginRight="55dp" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv3" 
    android:layout_toRightOf="@id/iv5" 
    android:layout_marginLeft="55dp" 
    android:layout_above="@id/iv5" 
    android:layout_marginBottom="55dp" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv9" 
    android:layout_toRightOf="@id/iv5" 
    android:layout_marginLeft="55dp" 
    android:layout_below="@id/iv5" 
    android:layout_marginTop="55dp" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv1" 
    android:layout_toLeftOf="@id/iv5" 
    android:layout_marginRight="55dp" 
    android:layout_above="@id/iv5" 
    android:layout_marginBottom="55dp" 
    android:background="@drawable/square"/> 

<ImageView 
    android:layout_width="55dp" 
    android:layout_height="55dp" 
    android:id="@+id/iv7" 
    android:layout_toLeftOf="@id/iv5" 
    android:layout_marginRight="55dp" 
    android:layout_below="@id/iv5" 
    android:layout_marginTop="55dp" 
    android:background="@drawable/square"/> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tv1" 
    android:text="Default" 
    android:textSize="30sp" 
    android:background="@android:color/transparent" 
    android:textColor="#FFFF00"/> 

コード - 私のXMLがあります。 XとYの値を取得するには? は、ここに私のクラスのコード -

package com.example.sid.rcs20; 

import android.content.Context; 
import android.content.pm.PackageManager; 
import android.graphics.Bitmap; 
import android.graphics.Color; 
import android.graphics.ImageFormat; 
import android.graphics.Matrix; 
import android.graphics.drawable.ColorDrawable; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.renderscript.Allocation; 
import android.renderscript.Element; 
import android.renderscript.RenderScript; 
import android.renderscript.ScriptIntrinsicYuvToRGB; 
import android.renderscript.Type; 
import android.support.v7.app.AppCompatActivity; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.View; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.Toast; 

import java.util.List; 

public class Cameras extends AppCompatActivity implements Camera.PreviewCallback{ 

private Camera mCamera; 
private CameraPreview mPreview; 
static int width, height; 
private ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8, iv9; 
private FrameLayout frameLayout; 
private String p51 ,p52, p53, p54, p55; 


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

    frameLayout = (FrameLayout) findViewById(R.id.camera_preview); 
    iv1 = (ImageView) findViewById(R.id.iv1); 
    iv2 = (ImageView) findViewById(R.id.iv2); 
    iv3 = (ImageView) findViewById(R.id.iv3); 
    iv4 = (ImageView) findViewById(R.id.iv4); 
    iv5 = (ImageView) findViewById(R.id.iv5); 
    iv6 = (ImageView) findViewById(R.id.iv6); 
    iv7 = (ImageView) findViewById(R.id.iv7); 
    iv8 = (ImageView) findViewById(R.id.iv8); 
    iv9 = (ImageView) findViewById(R.id.iv9); 


    DisplayMetrics displayMetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
    height = displayMetrics.heightPixels; 
    width = displayMetrics.widthPixels; 

    Log.d("phone size", String.valueOf(width) + "\t" + String.valueOf(height)); 

    if (!checkCameraHardware(this)) { 
     Toast.makeText(this, "NO CAMERA DETECTED", Toast.LENGTH_SHORT).show(); 
    } else { 
     mCamera = getCameraInstance(); 


     Camera.Parameters params = mCamera.getParameters(); 

     List<Camera.Size> sizes = params.getSupportedPictureSizes(); 
     Camera.Size size = sizes.get(0); 
//Camera.Size size1 = sizes.get(0); 
     for (int i = 0; i < sizes.size(); i++) { 

      Log.d("all sizes", String.valueOf(sizes.get(i).width) + "\t" + String.valueOf(sizes.get(i).width)); 
      if (sizes.get(i).width > size.width) { 
       size = sizes.get(i); 

      } 


     } 

     Log.d("size", String.valueOf(size.width) + "\t" + String.valueOf(size.width)); 

     params.setPictureSize(size.width, size.height); 
       params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); 
     params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
     params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO); 
     params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO); 
     params.setExposureCompensation(0); 
     params.setPictureFormat(ImageFormat.JPEG); 
     params.setJpegQuality(100); 
     params.setRotation(90); 

     mCamera.setDisplayOrientation(90); 


     mCamera.setParameters(params); 
     mCamera.setPreviewCallback(this); 


     mPreview = new CameraPreview(this, mCamera); 
     frameLayout.addView(mPreview); 
    } 
} 


/** 
* Check if this device has a camera 
*/ 
private boolean checkCameraHardware(Context context) { 
    // this device has a camera 
// no camera on this device 
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); 
} 

/** 
* A safe way to get an instance of the Camera object. 
*/ 
public static Camera getCameraInstance() { 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } catch (Exception e) { 
     // Camera is not available (in use or does not exist) 
    } 
    return c; // returns null if camera is unavailable 
} 


/** 
* Create a File for saving an image or video 
*/ 


@Override 
protected void onPause() { 
    super.onPause(); 
    releaseCamera();    // release the camera immediately on pause event 
} 

private void releaseCamera() { 
    if (mCamera != null) { 
     mCamera.setPreviewCallback(null); 
     mPreview.getHolder().removeCallback(mPreview); 
     mCamera.release();  // release the camera for other applications 
     mCamera = null; 
    } 
} 


@Override 
public void onPreviewFrame(byte[] bytes, Camera camera) { 

    int frameHeight = mCamera.getParameters().getPreviewSize().height; 
    int frameWidth = camera.getParameters().getPreviewSize().width; 
    Log.d("preview size", String.valueOf(frameWidth) + "\t" + String.valueOf(frameHeight)); 

    Bitmap bitmap = Bitmap.createBitmap(frameWidth, frameHeight, Bitmap.Config.ARGB_8888); 
    Allocation bmData = renderScriptNV21ToRGBA888(
      this, 
      frameWidth, 
      frameHeight, 
      bytes); 
    bmData.copyTo(bitmap); 
    Bitmap bmp2 = RotateBitmap(bitmap, 90); 

    int pixel1 = bmp2.getPixel(145, 415); 
    int pixel2 = bmp2.getPixel(365, 415); 
    int pixel3 = bmp2.getPixel(585, 415); 
    int pixel4 = bmp2.getPixel(145, 630); 
    int pixel51 = bmp2.getPixel(365, 630); 
    int pixel52 = bmp2.getPixel(306, 570); 
    int pixel53 = bmp2.getPixel(425, 690); 
    int pixel54 = bmp2.getPixel(425, 570); 
    int pixel55 = bmp2.getPixel(306, 690); 
    int pixel6 = bmp2.getPixel(585, 630); 
    int pixel7 = bmp2.getPixel(145, 865); 
    int pixel8 = bmp2.getPixel(365, 865); 
    int pixel9 = bmp2.getPixel(585, 865); 

    changeRect(pixel1, 1); 
    changeRect(pixel2, 2); 
    changeRect(pixel3, 3); 
    changeRect(pixel4, 4); 
    changeRect(pixel51, 51); 
    changeRect(pixel52, 52); 
    changeRect(pixel53, 53); 
    changeRect(pixel54, 54); 
    changeRect(pixel55, 55); 
    changeRect(pixel6, 6); 
    changeRect(pixel7, 7); 
    changeRect(pixel8, 8); 
    changeRect(pixel9, 9); 

} 


private void changeRect(int pixel, int num) { 
    int r = Color.red(pixel); 
    int g = Color.green(pixel); 
    int b = Color.blue(pixel); 

    ColorUtil colorUtil = new ColorUtil(); 
    String name = colorUtil.getColorNameFromRgb(r, g, b); 

    String[] idAndOurName = getDrawableIdAndOurName(name); 
    int d = Integer.parseInt(idAndOurName[0]); 
    String ourName = idAndOurName[1]; 



    if (num == 1) { 
     iv1.setImageResource(d); 
    } else if (num == 2) { 
     iv2.setImageResource(d); 
    } else if (num == 3) { 
     iv3.setImageResource(d); 
    } else if (num == 4) { 
     iv4.setImageResource(d); 
    } else if (num == 51) { 
     p51 = ourName; 
    } else if (num == 52) { 
     p52 = ourName; 
    } else if (num == 53) { 
     p53 = ourName; 
    } else if (num == 54) { 
     p54 = ourName; 
    } else if (num == 55) { 
     p55 = ourName; 
     d = getDrawableId(); 
     iv5.setImageResource(d); 
    } else if (num == 6) { 
     iv6.setImageResource(d); 
    } else if (num == 7) { 
     iv7.setImageResource(d); 
    } else if (num == 8) { 
     iv8.setImageResource(d); 
    } else if (num == 9) { 
     iv9.setImageResource(d); 
    } 

    Log.d("Selected Color", String.valueOf(num) + "\t" + name); 

} 

private int getDrawableId(){ 
    int[] cA = new int[]{0,0,0,0,0,0}; 
    cA = getCount(p51 , cA); 
    cA = getCount(p52 , cA); 
    cA = getCount(p53 , cA); 
    cA = getCount(p54 , cA); 
    cA = getCount(p55 , cA); 


    if (cA[0]>=2){ 
     return R.drawable.yellow; 
    } 
    else if (cA[1]>=2){ 
     return R.drawable.green; 
    } 
    else if (cA[2]>=2){ 
     return R.drawable.blue; 
    } 
    else if (cA[3]==5){ 
     return R.drawable.white; 
    } 
    else if (cA[5]>=1){ 
     return R.drawable.red; 
    } 
    else if (cA[4]>=2){ 
     return R.drawable.orange; 
    } 

    else 
     return R.drawable.square; 
} 

private int[] getCount(String s, int[] countArray){ 
    switch (s) { 
     case "y": 
      countArray[0]++; 
      break; 
     case "g": 
      countArray[1]++; 
      break; 
     case "b": 
      countArray[2]++; 
      break; 
     case "w": 
      countArray[3]++; 
      break; 
     case "o": 
      countArray[4]++; 
      break; 
     case "r": 
      countArray[5]++; 
      break; 
    } 
    return countArray; 
} 

private String[] getDrawableIdAndOurName(String name){ 
    if (name.contains("Yellow")) { 
     return new String[]{String.valueOf(R.drawable.yellow), "y"}; 
    } 
    else if (name.contains("White")) { 
     return new String[]{String.valueOf(R.drawable.white), "w"}; 
    } 
    else if (name.contains("Green")) { 
     return new String[]{String.valueOf(R.drawable.green), "g"}; 
    } 
    else if (name.contains("Blue")) { 
     return new String[]{String.valueOf(R.drawable.blue), "b"}; 
    } 
    else if (name.contains("Red")) { 
     return new String[]{String.valueOf(R.drawable.red), "r"}; 
    } 
    else if (name.contains("Orange")) { 
     return new String[]{String.valueOf(R.drawable.orange), "o"}; 
    } 
    else return new String[]{"" , ""}; 
} 

public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) { 
    RenderScript rs = RenderScript.create(context); 
    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); 

    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length); 
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); 

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height); 
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); 

    in.copyFrom(nv21); 

    yuvToRgbIntrinsic.setInput(in); 
    yuvToRgbIntrinsic.forEach(out); 
    return out; 
} 

public static Bitmap RotateBitmap(Bitmap source, float angle) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
} 


public void onCapClick(View view) { 
    mCamera.stopPreview(); 
    frameLayout.setForeground(new ColorDrawable(Color.WHITE)); 
} 

}

がimageviewsの中心の正確な位置を得るために私を助けてくださいです。

答えて

0

私は(画面の)中央ポイントの座標と中心ImageViewのの座標を取るならば、彼らは異なっている。この

int test1[] = new int[2];  
contentsView.getLocationInWindow(test1); 
//Or 
int test2[] = new int[2]; 
contentsView.getLocationOnScreen(test2); 
+0

をお試しください –

関連する問題